2014-06-21 100 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 
}