簡體   English   中英

使用PHP從Mysql顯示圖像

[英]Displaying Image from Mysql with PHP

這就是我的表在數據庫中的樣子。 我正在嘗試顯示存儲的圖像是mimetype(longblob)。 當我運行代碼時,它給了我一個帶有?的小盒子。 ,僅此框沒有錯誤。 有誰知道錯誤是什么以及我該如何解決?

Display
+-------+------------+----------+
| Index | Display_ID | Picture  |
+-------+------------+----------+
|     1 |         12 | longblob |
+-------+------------+----------+


<?php
    $mysqli=mysqli_connect('localhost','root','','draftdb');


    if (!$mysqli)
        die("Can't connect to MySQL: ".mysqli_connect_error());

    $imageid= 12;

    $stmt = $mysqli->prepare("SELECT PICTURE FROM display WHERE DISPLAY_ID=$imageid"); 
    $stmt->bind_param("i", $imageid);

    $stmt->execute();
    $stmt->store_result();

    $stmt->bind_result($image);
    $stmt->fetch();

    header("Content-Type: image/jpeg");
    echo $image; 
?>

這個:

$stmt = $mysqli->prepare("SELECT PICTURE FROM display WHERE DISPLAY_ID=$imageid");

應該:

$stmt = $mysqli->prepare('SELECT PICTURE FROM display WHERE DISPLAY_ID=?');

您是直接在查詢中嵌入變量,而不是像預期的那樣實際使用綁定變量。

它不是直接回答這個問題,但是通常這不是您的操作方式。

通常,您會將圖像的路徑存儲在數據庫中(也許作為varchar字段),然后照常加載圖像。 這具有以下優點:簡單,使數據庫保持較小且更易於版本控制,將常規緩存規則應用於映像等。

缺點是任何人都可以查看圖像,這可能會或可能不會引起問題。

如果您需要沿着開始的路線走,請首先注釋掉header("Content-Type: image/jpeg"); 並查看什么是PHP錯誤。 這可能會有所幫助。

也必須指定一個內容長度標頭:

header("Content-Length: ".strlen($image));
header("Content-Type: image/jpeg");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM