如何使用 PHP 中的准备语句显示数据库中的图像?
How to display images from the database using prepared statments in PHP?
我正在构建一个博客应用程序,用户可以在其中 post 博客和上传图片。并且博客的功能之一是在 header 中显示数据库中当前用户的个人资料图像。我正在使用 PHP 的程序样式和准备好的语句来显示图像,但我得到的是损坏的图像 link 而不是图像本身。
这是我使用 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 语句中使用它时遇到问题。
行内
mysqli_stmt_bind_param($stmt, "s", $profile_picture);
你应该用 $username
替换变量 $profile_picture
我正在构建一个博客应用程序,用户可以在其中 post 博客和上传图片。并且博客的功能之一是在 header 中显示数据库中当前用户的个人资料图像。我正在使用 PHP 的程序样式和准备好的语句来显示图像,但我得到的是损坏的图像 link 而不是图像本身。 这是我使用 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 语句中使用它时遇到问题。
行内 mysqli_stmt_bind_param($stmt, "s", $profile_picture); 你应该用 $username
替换变量 $profile_picture