我有一个代码,之前我已经反复使用过,但现在它变得困惑了。我只想将数据库中的信息列出到页面上的表格中,但现在它只会显示一个结果,而不是它找到的所有结果。
<table>
<tr><td style="background-color:#009745; color:#FFFFFF"><center><strong>Address Book</strong></center></td></tr>
<tr>
<?php
$getids = mysql_query("SELECT id, first_name, last_name FROM accounts WHERE s1='$id' ORDER BY id DESC", $db);
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
?>
<td><a href="#"><?= $ab_id ?></a> - <?= $ab_fn . " " . $ab_ln ?></td>
<?php
} else {
?>
<td><center>No Contacts</center></td>
<?php
}
?>
</tr>
</table>
请帮我解决这个问题。
<小时/>感谢您的帮助:)
我喜欢这个网站!当我需要的时候我总能得到答案。
最佳答案
我发现有两处错误
- 您正在使用 mysql_fetch_array,稍后您将使用字符串索引来打印结果
打印循环中的内容,它覆盖值并仅存储最后一行
if (mysql_num_rows($getids) > 0) { while ($gids = mysql_fetch_assoc($getids)) { $ab_id = $gids['id']; $ab_fn = $gids['first_name']; $ab_ln = $gids['last_name']; echo '<td><a href="#">'.$ab_id.'</a> -'. $ab_fn.''.$ab_ln.' </td>'; }
关于php - mysql 获取数组不再列出所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398299/