仅回显会话个人资料图片而不是整个数据库图片

Echo only the sessions profile picture instead of the entire databases pictures

现在我的代码正在回显数据库中的所有用户及其个人资料图片。我的目标是仅回显会话用户名和个人资料图片。任何帮助,将不胜感激。谢谢

<?php

        $q = mysqli_query($db, "SELECT * FROM users");
        while($row = mysqli_fetch_assoc($q)) {
            echo $username['username'];
                        if($row['image'] == ""){
                            echo "<img width='100 height='100' src='/student/globalit/2019/GamerMedia/pages/images/ao.jpg' alt ='Default Profile Pic'>";

                        }
                        else{
                            echo "<img width='100' height='100' src='/student/globalit/2019/GamerMedia/pages/images/".$row['image']."' alt='Profile Pic'>";
                        }
                    echo "<br>";    
        }
        ?>  

如果我理解你的问题,那么你必须向 SQL 数组添加一个 WHERE 子句。我认为变量 $username 已经定义并且是一个包含当前用户数据的数组。我还假设用户名的 table 列命名为 "username"。所以你的工作解决方案可能是这样的:

<?php

    $q = mysqli_query($db, "SELECT * FROM users WHERE username='".$username['username']."' LIMIT 1");
    while($row = mysqli_fetch_assoc($q)) {
        echo $username['username'];
                    if($row['image'] == ""){
                        echo "<img width='100 height='100' src='/student/globalit/2019/GamerMedia/pages/images/ao.jpg' alt ='Default Profile Pic'>";

                    }
                    else{
                        echo "<img width='100' height='100' src='/student/globalit/2019/GamerMedia/pages/images/".$row['image']."' alt='Profile Pic'>";
                    }
                echo "<br>";    
    }
    ?>