如何使用PHP中的预准备语句显示数据库中的图像?

问题描述 投票:0回答:1

我正在构建一个博客应用程序,用户可以在其中发布博客和上传图像。该博客的一个功能是在标题中显示数据库中当前用户的个人资料图像。我使用PHP的程序风格和准备语句来显示图像,但我得到的是一个破碎的图像链接而不是图像本身。这是我使用SELECT语句和预处理语句获取图像的代码:

<?php 
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];

$stmt = mysqli_prepare($connection, "SELECT user_image FROM users WHERE user_name = ?");
mysqli_stmt_bind_param($stmt, "s", $profile_picture);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $profile_picture);
mysqli_stmt_fetch($stmt);
while (mysqli_stmt_fetch($stmt)) {
    $profile_picture = $stmt['user_image'];
}
mysqli_stmt_close($stmt);
}
 ?>

以下是我在显示数据库中正在选择的用户的图像的代码:

<img width='30' class='img-responsive; img-circle' src='../images/<?php echo $profile_picture; ?>'>

我的问题是如何使用程序样式PHP使用预准备语句正确显示数据库中的图像?我很擅长使用预处理语句并且在其中有一些挫折。我理解并知道如何使用INSERT语句使用预准备语句,但我在SELECT语句中使用它时遇到了麻烦。

php prepared-statement
1个回答
0
投票

在mysqli_stmt_bind_param行中($ stmt,“s”,$ profile_picture);你应该用$ username替换变量$ profile_picture

© www.soinside.com 2019 - 2024. All rights reserved.