2014-06-21 55 views
0

我有我的PHP while语句的问题。它是:PHP,而声明问题

$row = mysqli_fetch_array($result); 
$time_in_12_hour_format = date("g:i a", strtotime($row['Time'])); 
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results 
    echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name 

是的,我意识到我定义$行两次,但我不知道如何改变这一点。如果我从while语句中删除$ row = mysqli_fetch_array($ result),页面将不会加载并返回错误。截至目前,页面加载但表格是空的,尽管我知道有一个记录在那里显示在表格中,然后我添加了军事时间转换器。任何帮助表示赞赏。

+0

后完整的代码 –

+0

删除第一个'$行= mysqli_fetch_array($结果);',并添加'$ time_in_12_hour_format =日期( “G:IA” 的strtotime($行[”时间']));'循环。 – CnapoB

回答

0

试试这个

while ($row = mysqli_fetch_array($result)) { //Creates a loop to loop through results 
    $time_in_12_hour_format = date("g:i a", strtotime($row['Time'])); 
    echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; 
} 

你叫mysqli_fetch_array它将从表中的一行,所以既然你说你的表中有一个记录,一个记录将在第一mysqli_fetch_array通话并最终第二返回每次致电mysqli_fetch_array返回NULL,因为表中没有更多记录要提取

0

尝试将$time_in_12_hour_format移动到while循环中。

while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results 
    $time_in_12_hour_format = date("g:i a", strtotime($row['Time'])); 
    echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name 
} 

这也将解决定义$行两次,这是首先导致问题的原因。因为你已经叫mysqli_fetch_array($result)你的while循环在第2行开始。

0

$time_in_12_hour_format应该为每一行设置吧?如果是,则尝试在while循环内移动它。您当前的代码只会开始显示第2行,第1行将被忽略。修改后的代码应该是这样的:

while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results 
    $time_in_12_hour_format = date("g:i a", strtotime($row['Time'])); 
    echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name 
}