2012-10-22 59 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要走的路! –