gpt4 book ai didi

Php - 使用路径(PDO)显示数据库中的图像

转载 作者:行者123 更新时间:2023-11-29 11:40:39 24 4
gpt4 key购买 nike

我尝试显示数据库中的图像,

  • 目录中的图像
  • 数据库中的路径

    require_once "Connection.php";
    class DisplayDataImageProfile {
    function showImageProfile(){
    $connection = new Connection();
    $conn = $connection->getConnection();

    $id = $_GET['id'];

    try{
    $sqlDisplay = "SELECT photo FROM frofile WHERE id =$id";
    $getImage = $conn->prepare($sqlDisplay);
    $getImage->execute();
    $getImage->fetchAll(PDO::FETCH_ASSOC);

    foreach($getImage as $data){
    header('Content-type: image/jpg');
    // echo "<img src='$data'>";
    echo $data;
    }

    }catch (PDOException $e){
    echo "Error : " + $e->getMessage();
    }
    }}

之后我像这样调用 html 页面:

<img src="DisplayDataImageProfile .php?id=3" align="center" />

我遇到问题,无法使用路径从数据库检索图像。网页上的其他情况下,图像显示为损坏的图像。

目前我只显示一张图像。

最佳答案

您应该使用正确的方法。

您必须使用返回单个值的 fetchColumn(),而不是返回嵌套数组的 fetchAll()。并且您应该正确使用准备好的语句:

$sql = "SELECT photo FROM frofile WHERE id = ?";
$getImage = $conn->prepare($sqlDisplay);
$getImage->execute([$_GET['id']]);
header('Content-type: image/jpg');
echo $getImage->fetchColumn();

编辑:如果您的数据库中没有图像本身,而只有图像的路径,那么您根本不需要此代码。摆脱 Display.php 并只需在回显的脚本中选择您的路径

<img src="Display.php?id=3" align="center" />

并回显所选路径而不是 Display.php?id=3

关于Php - 使用路径(PDO)显示数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35837415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com