【问题标题】:Echo only the sessions profile picture instead of the entire databases pictures仅回显会话个人资料图片而不是整个数据库图片
【发布时间】:2020-02-15 16:44:11
【问题描述】:

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

<?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>";    
        }
        ?>  

【问题讨论】:

  • 听起来你只想显示当前用户,所以在你的SELECT * FROM users 中添加一个WHERE 子句。
  • $username 你在哪里定义的?
  • 试试:"SELECT * FROM users WHERE username = '".$username['username']."'"
  • 用户名定义为$username = $_SESSION['username'];,您添加的代码给了我这个错误:警告:/home/benrud/public_html/student/globalit/2019/GamerMedia/ 中的非法字符串偏移“用户名” pages/account.php 第 34 行

标签: php database session echo


【解决方案1】:

如果我正确理解您的问题,那么您必须在 SQL 数组中添加 WHERE 子句。我认为变量$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>";    
    }
    ?>  

【讨论】:

  • 我从中得到了这个错误:警告:第 34 行 /home/benrud/public_html/student/globalit/2019/GamerMedia/pages/account.php 中的非法字符串偏移“用户名”
  • 您是否检查了 sql Query username= ' " . $username['username'] 的正确附件。" '
猜你喜欢
  • 2021-03-20
  • 2018-08-16
  • 2014-06-03
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多