PHP problem

He has: 1,380 posts

Joined: Feb 2002

hey...I basically have a page at onlinelba.com/meets.html that has an iframe in it, that is php file....but when it loads the php file, nothing displays. I have used the php code before...here's the code (Suzanne did most of this coding for me):

<?php
...connection....

// get the six newest activities with a particular date format
$sql = mysql_query(\"SELECT DATE_FORMAT(time, '%l:%i %p'), activity, place, DATE_FORMAT(date, '%m/%d/%Y') AS date FROM meets ORDER BY date\") or die(mysql_error());

// set the variable for the activities
$actDate = '';

// get the activities from the array and set their display
while (
$qry = mysql_fetch_array($sql)) {
   
$date = $qry['date'];
   
$activity = $qry['activity'];
   
$place = $qry['place'];
   
$order = $qry['order'];
   
   
$today = date(Y-m-d);

    if (
$today <= $date) {
        // if activities are planned for the future or today, display them
       
$actDate .= \"<tr>
            <td class=\\"
tfill\\">
            </td>
            <td class=\\"
meet\\"><p class=\\"practice\\">$date</p>
            </td>
            <td class=\\"
meet2\\"><p class=\\"practice\\">$place</p>
            </td>
            <td class=\\"
meet2\\"><p class=\\"practice\\">$activity</p>
            </td>
        </tr>\";
    }
    else {
        // if there are no activities, write a blank row saying so
       
$actDate .= \"<tr>
            <td class=\\"
tfill\\">
            </td>
            <td class=\\"
meet\\"><p class=\\"practice\\">TBA</p>
            </td>
            <td class=\\"
meet2\\"><p class=\\"practice\\">TBA</p>
            </td>
            <td class=\\"
meet2\\"><p class=\\"practice\\">TBA</p>
            </td>
        </tr>\";
    }
}
...disconnect...

[start page]
?>

thanks.

Chroder's picture

He has: 91 posts

Joined: Mar 2004

$today = date(Y-m-d); '
Is the only possable thing I see wrong. Try changing to (notice the single-quotes)
$today = date('Y-m-d'); '

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Put in errors and see what's wrong --

if (!$sql) {echo mysql_error(); }

Also, for troubleshooting, echo out the results.

<?php
// everything okay so far? (or edit die to echo the error)
if (!$sql) { echo mysql_error(); exit; }

while (
$qry = mysql_fetch_array($sql)) {
   
$date = $qry['date'];
   
$activity = $qry['activity'];
   
$place = $qry['place'];
   
$order = $qry['order'];

   
$today = date(Y-m-d);

   
// is everything from here okay?
   
echo $date . \"<br>\";
    echo
$activity . \"<br>\";
    echo
$place . \"<br>\";
    echo
$order . \"<br>\";
    echo
$today . \"<br>\";
    exit;

}
?>

Then work your way through the code to see where it's breaking down. If nothing at ALL is displaying, then it's likely a problem with the $sql in the first place -- all the display items depend on that result coming through and you'll get nothing if it doesn't work.

Chroder's picture

He has: 91 posts

Joined: Mar 2004

No need for the "if $sql", Eskater05's already got an "or die" in place.

It'd be good to use mysql_num_rows() I think. Add this right after your query.

<?php
if(mysql_num_rows($sql) < 1)
{
    echo
'<b>No rows returned.</b>';
}
?>

Your right Suzanne, its probably because there's no results coming through. Why didnt I think of that Sticking out tongue

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Sorry, I just edited that, whoops!

The die() doesn't echo the error, so it's not returning anything to the browser?

Chroder's picture

He has: 91 posts

Joined: Mar 2004

A query that returns 0 results is still a perfectly valid query. So I'm guessing you were right when you said there were no rows.

Or try looking at the page source. It may look blank, but I know sometimes I've got errors but I don't see them because I've got them printed before the " ....".

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

The problem I think is the same code works (or near to the same code) on other pages on the site and there ARE rows (expected and actually there) for that query? Kyle, can you confirm what you've changed? I don't see a LIMIT on there... Is this old or new code?

Chroder's picture

He has: 91 posts

Joined: Mar 2004

And one more thing, how are you comparing the date string with a 'less-than or equal to' operator? Does PHP automatically convert date's or something? Not sure about that...

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://www.php.net/manual/en/function.date.php on how PHP does it
http://www.mysql.com/doc/en/DATETIME.html on how MySQL does it

DATE_FORMAT(date, '%m/%d/%Y') AS date sets the date in the m/d/Y format, so to compare it to the $today variable, it should be $today = date(m/d/Y); but I thought I put a LIMIT = 6 in the query and WHERE date >= $today blah blah??

He has: 1,380 posts

Joined: Feb 2002

Sorry...I've been busy...

Yes, I took your old one, and modified it for a new page, removing the LIMIT...different table, almost exactly the same info...40+ rows of information

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

So you don't need to restrict it by date? And you're accessing a different table? Okay, make sure you have all that information right, then, eh?

Do you have a sample of the table data?

Chroder's picture

He has: 91 posts

Joined: Mar 2004

Its from a different table? Thats weird, mysql should be spitting out 'unknown column' errors... Confused

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I'm assuming that the tables are set up with the same fields (or close)?

He has: 1,380 posts

Joined: Feb 2002

Yes, they are setup with the exact same fields...

I do have to restrict by date, just not limit how many results display

Sample:
{Order} {Activity} {Date} {Place} {Time}
1 Meet 2004-6-02 Mall 00:00:00
etc.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.