我需要幫助,我試圖抓取行,但不能將它追加到while
循環中的數組上:$rows[] = $row;
然後嘗試構建$rows array
,然後在隨後的foreach中輸出。
我該怎麼做?
<?php
$query = $dbc->query('SELECT * FROM customer');
$rows = array();
while ($row = $query->fetch_assoc()) {
echo $row['clientid'].' '.$row['inital'].' '.$row['firstname'].' '.$row['lastname'].' '.$row['mobile'].' '.$row['landline'].' '.$row['email'].' '.$row['address'].' '.$row['postcode'];
}
foreach ($rows as $row) {
?>
<td><?php $row['clientid']; ?></td>
<td><?php $row['inital']; ?></td>
<td><?php $row['firstname']; ?></td>
<td><?php $row['lastname']; ?></td>
<td><?php $row['mobile']; ?></td>
<td><?php $row['landline']; ?></td>
<td><?php $row['email']; ?></td>
<td><?php $row['address']; ?></td>
<td><?php $row['postcode']; ?></td>
</td>
<?php } ?>
最終,我就是這麼做的,
<?php
$query = $dbc->query('SELECT * FROM customer');
if (mysqli_num_rows($query) > 0)
{
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
echo "<tr><td>".$row['clientid']."</td><td>".$row['inital']."</td><td>".$row['firstname']."</td><td>".$row['lastname']."</td><td>".$row['mobile']."</td><td>".$row['landline']."</td><td>".$row['email']."</td><td>".$row['address']."</td><td>".$row['postcode']."</td></tr>";
}
echo '</tbody></table>' ;
}
else { echo '<div class="alert alert-error">
There are currently no records.
</div>' ; }
?>
所以就像這樣'$ query = $ dbc->查詢('SELECT * FROM customer'); $ rows = array(); while($ row = $ query-> fetch_assoc()) \t \t {$ rows [] = $ row; \t \t echo $ row ['clientid']。'' '$行[ 'inital'。' '$行[ '名字'。' '$行[ '姓氏'。' '$行[ '移動'。' '$行[ '固定電話'。' '$行[ '電子郵件'。' '$行[ '地址'。' '$行[ '郵政編碼'。' 」。$行[ '無障礙']; } foreach($ rows as $ row)' – KP2012 2013-02-20 11:35:44
是的。當然,通過將while循環與foreach循環相結合,你可以使它更有效率,因爲你基本上遍歷了兩行的列表。 – 2013-02-20 14:26:43
它不喜歡''等 – KP2012 2013-02-20 19:16:40