我有一个代码,我之前一遍又一遍地使用它现在它正在弄乱。我想要做的就是将数据库中的信息列表到页面上的表中,但现在它只显示一个结果,而不是它找到的所有结果。
<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>
请帮助我。
感谢您的帮助:)
我喜欢这个网站!当我需要时,我总能得到答案。
答案 0 :(得分:2)
我看到两件事错了
打印循环中的东西,它覆盖了值,只存储了最后一行
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>'; }
答案 1 :(得分:1)
在这个凌乱的代码中,你过早关闭了while
循环:
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
稍后仅使用最后检索的行。另外,如果您没有访问结果的数字,请不要使用mysql_fetch_array
。请改用mysql_fetch_assoc
。