0

Hi,

I've had a look through the forum(s), however, can't seem to find a solution to my issue (and I don't understand the PHP Manual).

Basically, got a website linked to a database, trying to pull data from one table and display onto the website where appropriate.

I have the following code:

<?php
    include "console/include/code/common.php";  
    //Connect to Database
    DBConnect();
    $Link = mysql_connect(dbhost,user,password);?>

    $Query = "SELECT * FROM database.tablename WHERE eventDay=1";
    $Result = mysql_query ($Query, $Link) or die (mysql_error($Link));

    //ON Debugging - code stops here resulting in Resource ID #3 - suggesting there is a problem
    with the $Result. However, please check the following code to see if there are errors (if poss).

    $Rows=mysql_num_rows($Result) or die (mysql_error($Rows));
    $loop=0;
    while ($loop<$Rows){
            //Add all variables to the output loop.
            $eventVisible=mysql_result($Result,$loop,"eventVisible");
            $eventDay=mysql_result($Result,$loop,"eventDay");
            $eventImagePath=mysql_result($Result,$loop,"eventImagePath");
            $eventTitle=mysql_result($Result,$loop,"eventTitle");
            $eventInfo=mysql_result($Result,$loop,"eventInfo");
            $expiryYear=mysql_result($Result,$loop,"expiryYear");
            $expiryMonth=mysql_result($Result,$loop,"expiryMonth");
            $expiryDay=mysql_result($Result,$loop,"expiryDay");
        //Print out the values into a table
    }

    if($eventVisible==1){
        //EVENT VISIBLE
            date_default_timezone_set('Europe/London');
                if(mktime() < mktime(23,59,59,$expiryMonth,$expiryDay,$expiryYear)) {
                //Event is before expiry?>
                <img src="<?php echo $eventImagePath;?>" width="160" height="160" style="float:left"/> <p><b><?php echo $eventTitle;?></b><p><font color="#000"><?php echo $eventInfo;?></font>   
                <?php }; ?>
        <?php };?>
        <br clear="all">  
        <br>
        <?php
        //Increment the loop by 1 - so we actually get to an end!
        $loop++;
        //};?>
    </div>

This code is for one tab - there are 6, so i simply copy the code, and change the query based on the tab that's selected.
As you can probably tell, I've tried to cut down and explain the code as much as possible - any questions though, feel free to ask.

3
Contributors
12
Replies
26
Views
4 Years
Discussion Span
Last Post by minitauros
Featured Replies
  • What happens if you replace your mysql_result() functions by a more commonly used mysql_fetch_assoc()? E.g. replace $Query = "SELECT * FROM database.tablename WHERE eventDay=1"; $Result = mysql_query ($Query, $Link) or die (mysql_error($Link)); //ON Debugging - code stops here resulting in Resource ID #3 - suggesting there is a problem with … Read More

0

So does your "OR die(...)" function get triggered, or does it pass that? Maybe your query did not return any results?

Edited by minitauros

0

I echoed the query out and copy/pasted that into PHPMyAdmin and it returns 3 results as it should.

With regards to the "OR die(..)", when doing debugging I just put echo statements in.

 $Link = mysql_connect(dbhost,user,password);?>
 echo "Connection";
 $Query = "SELECT * FROM database.tablename WHERE eventDay=1";
 echo "Query"; 
 $Result = mysql_query ($Query, $Link) or die (mysql_error($Link));
 echo $Result;
 $Rows=mysql_num_rows($Result) or die (mysql_error($Rows));
 echo "Rows";

Based on these, I believe that it must pass the first or die ($Result = mysql_query....) but was producing Resource id #3 in the echo to this (as Rows never got displayed).

0

Paulkd - I have a common file, which the contents are:

<?php
function DBConnect(){
    the database admin tool.
    global $TestDBConnection, $DBHost, $DBUser, $//add further global vars at the end of the line below once you have created them in DBPassword, $DBName, $salt, $Table_1, $Table_2, $Table_3;

    $DBHost = "host";
    $DBUser = "dbuser";
    $DBPassword = "pwd";
    $DBName = "dbname";
    $salt=sha1("saltcombinationgoeshere");  
    //Tables listed here, add links to your new tables here too
    $Table_1 = "click_members";
    $Table_2 = "click_notices";
    $Table_3 = "click_guide";
    //Comment out this block when connection is successful.
    /*if (mysql_connect($DBHost,$DBUser,$DBPassword)){
        echo "Connection Active";
        }else{
        echo "Connection Error - Check common.php file";
    }*/
} //Close function
?>

and minitauros the only result that is displaying is "Resource ID #3"

0

I'm looking closer at your code original and it's making less sense. Help me out.

Line 5 - you have ended your php block with ?> so how is line 7 going to work ?

1

What happens if you replace your mysql_result() functions by a more commonly used mysql_fetch_assoc()? E.g. replace

 $Query = "SELECT * FROM database.tablename WHERE eventDay=1";
$Result = mysql_query ($Query, $Link) or die (mysql_error($Link));
//ON Debugging - code stops here resulting in Resource ID #3 - suggesting there is a problem
with the $Result. However, please check the following code to see if there are errors (if poss).
$Rows=mysql_num_rows($Result) or die (mysql_error($Rows));
$loop=0;
while ($loop<$Rows){
    //Add all variables to the output loop.
    $eventVisible=mysql_result($Result,$loop,"eventVisible");
    $eventDay=mysql_result($Result,$loop,"eventDay");
    $eventImagePath=mysql_result($Result,$loop,"eventImagePath");
    $eventTitle=mysql_result($Result,$loop,"eventTitle");
    $eventInfo=mysql_result($Result,$loop,"eventInfo");
    $expiryYear=mysql_result($Result,$loop,"expiryYear");
    $expiryMonth=mysql_result($Result,$loop,"expiryMonth");
    $expiryDay=mysql_result($Result,$loop,"expiryDay");
    //Print out the values into a table
}

by

$q = 'SELECT * FROM database.tablename WHERE eventDay=1';
$r = mysql_query($q) or exit(mysql_error());
$number_of_results = mysql_num_rows($r);

while($fetch = mysql_fetch_assoc($r))
{
    //* Looping through results

    $eventVisible = $fetch['eventVisible'];
    $eventDay = $fetch['eventDay'];
    // And so on
}
0

Ah that has worked a treat (with a little extra tweaking)!

Thank you so much minitauros!
EDIT - Thought it did :\ however, now the time function has stopped working correctly - however, I believe this to be an issue with the version of PHP I'm using?

Working code:

    <?php    //Start session, grab db and table variables and ask for messages to be displayed.
    include "console/include/code/common.php";  
    //Connect to Database
    DBConnect();
    $Link = mysql_connect($DBHost,$DBUser,$DBPassword);?>
    ...
    <?php $q = 'SELECT * FROM dbname.tablename WHERE eventDay=1';
    date_default_timezone_set('Europe/London');
    $r = mysql_query($q) or exit(mysql_error());
    $number_of_results = mysql_num_rows($r);
    while($fetch = mysql_fetch_assoc($r))
        {
        //* Looping through results
        $eventVisible = $fetch['eventVisible'];
        $eventDay = $fetch['eventDay'];
        $eventImagePath=$fetch['eventImagePath'];
        $eventTitle=$fetch['eventTitle'];
        $eventInfo=$fetch['eventInfo'];
        $expiryYear=$fetch['expiryYear'];
        $expiryMonth=$fetch['expiryMonth'];
        $expiryDay=$fetch['expiryDay'];

        if($eventVisible==1){
            if(mktime() > mktime(23,59,59,$expiryMonth,$expiryDay,$expiryYear)) {?>
                <img src="<?php echo $eventImagePath;?>" width="160" height="160" style="float:left"/> <p><b><?php echo $eventTitle;?></b><p><font color="#000"><?php echo $eventInfo;?></font>
                <br clear="all">  
                <br>
        <?};};}?>

Edited by maharrington: Tested code, found a part doesn't work

0

Did you check if $expiryMonth, $expiryDay and $expiryYear actually have values when you use them in the mktime() function? Also, if you don't have any arguments at all to pass to mktime(), you ought to use time() instead, which returns the current timestamp just as mktime(), but that's just strict standards for as far as I know :p.

0

Yeah they do, they automatically default to 31/Dec/2099 if a different one isnt entered.
What I'm trying to do is:
If the Visible variable = 1 (yes) then
If the expiry date greater than now,
Show result
Else show nothing
Else show nothing

Which i believe is what the code I'm using is doing?

Edited by maharrington

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.