我从数据库中获取时间,当我将其回显时,它可以工作,但在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
}

最佳答案

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

$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];

关于php - PHP时间变量在while循环中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19238137/

10-09 15:01