Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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中使用准备好的语句显示数据库中的图像?_Php_Prepared Statement - Fatal编程技术网

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

如何在PHP中使用准备好的语句显示数据库中的图像?,php,prepared-statement,Php,Prepared Statement,我正在构建一个博客应用程序,用户可以在其中发布博客和上传图像。博客的一个特点是在标题中显示数据库中当前用户的配置文件图像。我使用PHP的程序风格和准备好的语句来显示图像,但我得到的是一个断开的图像链接,而不是图像本身。 以下是我使用SELECT语句和prepared语句获取图像的代码: <?php if (isset($_SESSION['username'])) { $username = $_SESSION['username']; $stmt = mysqli_prepare($

我正在构建一个博客应用程序,用户可以在其中发布博客和上传图像。博客的一个特点是在标题中显示数据库中当前用户的配置文件图像。我使用PHP的程序风格和准备好的语句来显示图像,但我得到的是一个断开的图像链接,而不是图像本身。 以下是我使用SELECT语句和prepared语句获取图像的代码:

<?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语句中使用它时遇到了问题

排队 mysqli_stmt_bind_param($stmt,“s”,$profile_picture);
您应该将变量$profile\u picture替换为$username

您还可以将$profile\u picture变量初始化为默认图片,以防数据库返回任何内容。我要去工作。我犯了一个愚蠢的错误,在发帖之前没有注意到。再次感谢,@guy ribak。。。