我的脚本只会从sql数据库中打印一行。我想显示具有名字和姓氏的多行。这是一个朋友列表。代码如下。
谢谢你, 红色。
<?php
$q = "SELECT * FROM `friendslist` WHERE `id1` = '{$_SESSION['id']}' OR `id2` = '{$_SESSION['id']}' AND `confirmed` = '1'";
$res = mysqli_query($db, $q) or die("<h1>4:)Fatal Error! Please contact administration.</h1>".mysql_error());
while($friends = mysqli_fetch_array($res)){
if($_SESSION['id']==$friends['id1']){
$q = "SELECT * FROM `clientusers` WHERE `id` = '{$friends['id2']}' ORDER BY `id` ASC";
$res = mysqli_query($db, $q);
while($list = mysqli_fetch_array($res)){?>
<li class="chat__human">
<img class="chat__avatar" src="https://robohash.org/joe" alt="" />
<span class="chat__name"><?php print($list['firstname']);?> <?php print($list['lastname']);?></span>
</li>
<?}
}if($_SESSION['id']==$friends['id2']){
$q = "SELECT * FROM `clientusers` WHERE `id` = '{$friends['id1']}' ORDER BY `id` ASC";
$res = mysqli_query($db, $q);
while($list = mysqli_fetch_array($res)){?>
<li class="chat__human">
<img class="chat__avatar" src="https://robohash.org/joe" alt="" />
<span class="chat__name"><?php print($list['firstname']);?> <?php print($list['lastname']);?></span>
</li>
<?}
}
}
?>
答案 0 :(得分:0)
我认为问题在于变量&#34; $ res&#34;在下面的陈述中
while($friends = mysqli_fetch_array($res)){
被以下声明覆盖
$q = "SELECT * FROM `clientusers` WHERE `id` = '{$friends['id2']}' ORDER BY `id` ASC";
$res = mysqli_query($db, $q);
因此,当外部while循环返回以获取第二个记录时,它将显示一个新结果。因此,尝试命名资源变量$ res1和$ res2以避免混淆。
所以代码看起来像
$res1 = mysqli_query($db, $q) or die("<h1>4:)Fatal Error! Please contact administration.</h1>".mysql_error());
while($friends = mysqli_fetch_array($res1)){
....
....
....
$res2 = mysqli_query($db, $q);
while($list = mysqli_fetch_array($res2)){?>