【问题标题】:Trying to retrieve users profile picture from mysql database.试图从 mysql 数据库中检索用户个人资料图片。
【发布时间】:2017-12-13 11:51:10
【问题描述】:

我在尝试检索存储在 MYSQL 数据库中的用户配置文件链接时遇到问题。

我尝试运行此代码来选择用户配置文件(存储用户配置文件图片链接的位置),但出现 500 错误。

        <?php
    $stmt = $DB_con->prepare('SELECT id, userprofile FROM users ');
    $stmt->execute();
if($stmt->rowCount() > 0)
{
    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);
        ?>

链接存储在 userprofile 列中。

HTML(function.php 中有数据库信息):

<?php 
    include('functions.php');


    if (!isAdmin()) {
        $_SESSION['msg'] = "You must log in first";
        header('location: ../login.php');
    }


?>

<!DOCTYPE html>
<html>
<head>
    <title> Admin Home</title>

</head>
<body>
    <div class="header">
        <h2>Admin - Home Page</h2>
    </div>
    <div class="content">
        <!-- notification message -->


        <!-- logged in user information -->
        <div class="profile_info">
            <img src="uploads/<?php echo $row['userprofile']; ?>" class="img-rounded" width="250px" height="250px" />

            <div>
                <?php  if (isset($_SESSION['user'])) : ?>
                    <strong><?php echo $_SESSION['user']['username']; ?></strong>

                    <small>
                        <i  style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i> 
                        <br>
                        <a href="home.php?logout='1'" style="color: red;">logout</a>
                        &nbsp; <a href="create_user.php"> + add user</a>
                    </small>

                <?php endif ?>
            </div>
        </div>



    </div>

</body>
</html>

测试用户登录的图片(Bigfella)

关于为什么它没有从数据库中检索图像链接的任何想法?提前致谢!!

【问题讨论】:

  • 保存用户头像UserId.jpg。然后你可以运行if(file_exists('UserId.jpg'){ $profpic = 'UserId.jpg';} else {$profpic = 'defaultprofpic.jpg'}你不想和sql兄弟

标签: php mysql


【解决方案1】:

您需要一个} 来关闭while() 循环,另一个} 来关闭if 语句。

通常要找出 PHP 500 错误,如果您无权更改错误报告或在命令行模式下运行(大多数共享主机不会提供任何选项),请删除您的代码语句(或二进制 1/2 你的语句)一次,直到你有一部分工作并一次添加一行,希望你能找到语法错​​误。 500 可能意味着很多事情,但对于 PHP,我发现它通常意味着致命的语法错误。

【讨论】:

  • 喜欢这个吧? $stmt = $DB_con-&gt;prepare('SELECT id, userprofile FROM users '); $stmt-&gt;execute(); if($stmt-&gt;rowCount() &gt; 0) { } while($row=$stmt-&gt;fetch(PDO::FETCH_ASSOC)) { } extract($row); 我仍然遇到无法处理请求的 500 错误。
  • 没有。 { } 包围正在执行的语句。使用您的原始代码,这将在最后添加两个 } ?&gt;
  • 好的,我在关闭 php 之前放了两个 } ?> 它仍然出现 500 错误。 $stmt = $DB_con-&gt;prepare('SELECT id, userprofile FROM users '); $stmt-&gt;execute(); if($stmt-&gt;rowCount() &gt; 0) { while($row=$stmt-&gt;fetch(PDO::FETCH_ASSOC)) { extract($row); } }
  • 那么你需要进一步分解它。由于它相当短,一次尝试一个语句 - 即,首先只是准备(),然后添加执行,然后是 if 但有一个空的主体(即,没有 while),然后添加没有提取的 while。一旦您准确缩小从“无错误运行”到“500”的范围,您就可以发布该信息。调试 101。
【解决方案2】:

500 html error 表示服务器在尝试提供页面时出现错误。

这意味着您可能在

中生成了错误的url

&lt;img src="uploads/&lt;?php echo $row['userprofile']; ?&gt;" class="img-rounded" width="250px" height="250px" /&gt;

或者在服务器端有一个覆盖,使得对某个 url 路径的每次调用都由脚本提供服务。

如果您使用的是 Apache/HTTPD,请尝试检查您的 .htaccess 文件

【讨论】:

    【解决方案3】:

    1.) 500 错误是服务器错误。这里的原因似乎是语法不正确。试试这个:

    <?php
        ...
    
        if($stmt->rowCount() > 0)
        {
            // Since you want only one row (one user) you can drop the while
            $row=$stmt->fetch(PDO::FETCH_ASSOC);
            extract($row);
        }
    ?>
    

    2.) 为什么要加载来自所有用户的 iduserprofile 值?

    由于您一次只显示一个用户的信息,因此最好使用像这样更有选择性的东西:

    $stmt = $DB_con->prepare('SELECT `id`, `userprofile` FROM users WHERE `email`='.$clean['emailAddress']);
    

    【讨论】:

      【解决方案4】:

      我最终解决了 500 Error Unable to handle the request,而不是一开始就将代码移到 &lt;!DOCTYPE html&gt; 下方,它工作得非常好,没有错误,所以我不知道发生了什么但它现在正在工作!感谢所有帮助我尝试解决此问题的人! :)

      【讨论】:

        猜你喜欢
        • 2021-01-15
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        相关资源
        最近更新 更多