Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按id显示多个用户信息的问题_Php_Mysql - Fatal编程技术网

Php 按id显示多个用户信息的问题

Php 按id显示多个用户信息的问题,php,mysql,Php,Mysql,我试图从friends表中获取friends id,然后从users表中获取friends信息。我试过用foreach,但运气不好 这就是我现在所拥有的,它只是回应一个朋友,而不是我桌上的三个朋友。关于如何解决这个问题,有什么想法吗?也许我没有正确地使用for-each?提前谢谢你 <?php //Gets users information from users. $stmt = $DB_con->prepare('SELECT friendsid FROM friends

我试图从friends表中获取friends id,然后从users表中获取friends信息。我试过用foreach,但运气不好

这就是我现在所拥有的,它只是回应一个朋友,而不是我桌上的三个朋友。关于如何解决这个问题,有什么想法吗?也许我没有正确地使用for-each?提前谢谢你

<?php 
//Gets users information from users. 
$stmt = $DB_con->prepare('SELECT  friendsid FROM friends WHERE userid='.$_SESSION['user']['id']);
$stmt->execute();

if($stmt->rowCount() > 0) {
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    extract($row);
}   else {
    $friendsid = $row['friendsid'];
} 

$stmt = $DB_con->prepare('SELECT username,userprofile,status FROM users WHERE id='.$friendsid);
$stmt->execute();

if($stmt->rowCount() > 0) {
    while($row=$stmt->fetch(PDO::FETCH_ASSOC))  {
        extract($row);
        ?>  
            <div class="row">
                <div class="col-sm-3">
                    <div class="well">
                        <h4><strong><?php echo $row['username'];?></strong></h4>
                        <img src="images/profile/<?php echo $userprofile;?>" class="img-circle" height="70" width="70" alt="Avatar">
                    </div>
                </div>
                <div class="col-sm-9">
                    <div class="well">
                        <h3 class="text-left"><?php echo $row['status'];?></h3>
                    </div>
                </div>
            </div>

<?php
    }
} else {
?>

<?php
}
?>


“class=“img circle”height=“70”width=“70”alt=“Avatar”>

准备好的语句使用参数,您应该使用联接

$stmt = $DB_con->prepare('SELECT u.* FROM users AS u JOIN friends AS f ON u.id = f.friendsid WHERE f.userid = :user_id');
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    // Do whatever you want with $row['id'], $row['username'], $row['userprofile']
}

这不是准备好的语句应该使用的方式。参数化。我会去掉第4-20行,通过一个查询获得所有数据。使用
join
。不要使用
extract
@chris85,所以它应该是这样的?
从用户内部选择users.id,users.username,users.userprofile从用户加入用户的好友。id=friends.friendsid;
Somet是这样的。不确定你的数据结构。你也需要一个
where
。我还会提取行吗?以显示所有用户的朋友吗?@BigfellaHQ不,不提取。几乎从不提取。
$row
将拥有你的数据。不需要提取。你可以直接使用$row数组。啊,所以我现在可以调用它,就像
$行['username'];
?它将显示所有好友的用户名?是的,循环每次将显示一个用户名,其中包含$row['username']