简体   繁体   中英

MySQL Queries in PHP not returning true results

I currently have the following PHP file which is using MySQL and it should return with results from 00:00 to 23:00 (this is a timetable), however it is only returning 01:00 to 23:00:

<?PHP
 include("../panel/config.php");
 #// Timetable Clearup Variabls

$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);
$order = "SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time LIMIT 0 , 30";
$result = mysql_query($order);
$yesterdayd = date('F jS, Y', time()-86400);

    //SET XML HEADER
    header('Content-type: text/xml');

    //CONSTRUCT RSS FEED HEADERS
    $output = '<rss version="2.0">';
    $output .= '<channel>';
    $output .= "<title>Timetable - {$yesterdayd} </title>";
    $output .= '<description>Timetable.</description>';
        $output .= '<link>http://www.site.com</link>';
     while ($row = mysql_fetch_array($result)) {
    //BODY OF RSS FEED

   $output .= "<item><title>Timetable - {$yesterdayd}</title>
   <description>";
while ($row=mysql_fetch_array($result))
{
        $rowset[] = $row;
  //BODY OF RSS FEED
  $output .= htmlspecialchars($row['time']) . " " . htmlspecialchars($row['username']) . "<br/>";
}
$output .= '</description></item> ';
 }
    //CLOSE RSS FEED
   $output .= '</channel>';
   $output .= '</rss>';

    //SEND COMPLETE RSS FEED TO BROWSER
$filename = "timetable.xml";

                if (!$handle = fopen($filename, 'w')) {
            echo "Cannot open file ($filename)";
            exit;
            }

            // Write $somecontent to our opened file.
            if (fwrite($handle, $output) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
            }

            if (fwrite($handle, $total) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
            }

            echo "Success, wrote {$content}{$total} to file ($filename)";

            fclose($handle);


?>

Can anyone shed some light

You are overwriting the first result due to the double while loop.

while ($row = mysql_fetch_array($result)) {
    //BODY OF RSS FEED

    $output .= "<item><title>Timetable - {$yesterdayd}</title><description>";
    while ($row=mysql_fetch_array($result))
    {
        // First $row result is now overwritten before you used it
        // ...

Also, you may want to use date('Ym-d') instead of date('l') . I'm not sure what your day column contains, but date('l') returns the name of the day which doesn't discriminate between Tuesday yesterday and Tuesday last week and Tuesday 5 years ago.

You only need one while($row = mysql_fetch_array($result)) instead of two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM