2012-10-22 57 views
-1

有沒有人看到這個錯誤?我沒有得到從第二個循環打印的結果。第二個循環頭文件如何被打印沒有問題。雖然循環數據不打印在屏幕上 - PHP

我試圖從SQL數據庫打印數據。

<?php 

$con= new mysqli('localhost','root','','regional_data'); 
if (mysqli_connect_errno()) {exit('Connection failed: '. mysqli_connect_error());} 
$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'"); 

echo "<table cellpadding='2' class='tablet' cellspacing='0'>"; 
echo 
"<tr> 
<th></th>" 
."<th>"."Starting Cheque No"."</th>" 
."<th>"."Ending Cheque No"."</th>" 
."<th>"."Total No of Cheques remaining"."</th>" 
."<th>"."Cheque Type"."</th>" 
."</tr>"; 

while ($reca = mysqli_fetch_array($result)) 
{ 
echo "<tr>"; 
echo "<td><input type='checkbox' ></td>"; 
echo "<td>".trim($reca["sbstart"])."</td>"; 
echo "<td>".trim($reca["sbend"])."</td>"; 
echo "<td>".trim($reca["totsb"])."</td>"; 
echo "<td>SB</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

     echo "<table cellpadding='2' class='tablet' cellspacing='0'>"; 
     echo 
     "<tr> 
     <th></th>" 
     ."<th>"."Starting Cheque No"."</th>" 
     ."<th>"."Ending Cheque No"."</th>" 
     ."<th>"."Total No of Cheques remaining"."</th>" 
     ."<th>"."Cheque Type"."</th>" 
     ."</tr>"; 
     while ($reca = mysqli_fetch_array($result)) 
     { 
     echo "<tr>"; 
     echo "<td><input type='checkbox' ></td>"; 
     echo "<td>".trim($reca["gwstart"])."</td>"; 
     echo "<td>".trim($reca["gwend"])."</td>"; 
     echo "<td>".trim($reca["totgw"])."</td>"; 
     echo "<td>GW</td>"; 
     echo "</tr>"; 
     } 
     echo "</table>"; 


$con->close(); 
?> 
</div> 

回答

2
while ($reca = mysqli_fetch_array($result)) 

這將提取所有結果從結果集。之後,結果集耗盡,這就是循環結束的原因。之後沒有更多的結果從相同的結果集中獲取。

要麼發出一個新的查詢,要麼將數據保存到一個數組中,您可以根據需要多次循環。

+0

男人哦,是的!謝謝! –

+0

實際上,當在while循環的上下文中使用時,它遍歷項目並使用數組中的當前項目 –

0

我不認爲你可以兩次使用相同的$結果變量。

我會做的是以下幾點:

$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'"); 
$result2 = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'"); 

那麼你的第一while循環可以使用mysqli_fetch_array($result),第二個可以使用mysqli_fetch_array($result2)

希望這會有所幫助!

+0

是Jo Hamm要走的路! –