0

我从我的数据库中获取时间,当我回显它时它可以工作,但它在 while 循环中不起作用。但是,其他变量起作用...

代码:

$resultevent = mysql_query('SELECT  venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
$rowevent = mysql_fetch_assoc($resultevent);

$date = new DateTime($originaldate);
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime'];
$endTime2 = $rowevent['endTime'];
$venue = $rowevent['venue'];

echo $startTime2; <============== WORKS HERE

while ($row = mysql_fetch_assoc($result)) { //the code for these queries is not shown
   echo 'getting here'; <======= WORKS
   echo $venue; <======= WORKS
   echo $startTime2; <======= DOESNT WORK
}
4

2 回答 2

1

您的查询正在返回带有别名的时间列。要从您的行中读取它们,您应该使用别名作为索引:

$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
于 2013-10-08T02:35:13.350 回答
1

1.在你的循环中是$results从哪里来的??2.您在查询中将 startTime 设置为 startTime2,与 endTime 设置为 endTime2 相同。很明显,即使是你的 .. 它也行不通echo $venue,因为你什么也得不到。

它应该是这样的代码:

    $date = new DateTime($originaldate);
    $resultevent = mysql_query('SELECT  venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');

    while ($rowevent = mysql_fetch_assoc($resultevent)) { //now you fetch your query in $resultevent

       $newdate = $date->format('m/d/Y');
       $startTime2 = $rowevent['startTime2'];
       $endTime2 = $rowevent['endTime2'];
       $venue = $rowevent['venue'];

       echo 'getting here';
       echo $venue; 
       echo $startTime2;
    }
于 2013-10-08T02:50:52.947 回答