如何通过从mysql数据库中检索图像来显示html中的图像。
我有以下代码,但只显示文本代替图像。 如何显示图像而不是图像文本。
码:
while ($row = mysql_fetch_assoc($result)) { //echo "<div>"; //echo "<div class=\"slide-image\">"; print $row['Image']; //echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; //echo "</div>"; //echo "</div>"; }它将生成的JPEG显示为文本,您可能会想到这很难解释。
how to display image in html by retrieving it from mysql database.
i have the following code but only text is being displayed in place of image. How to display the image instead of the text of image.
Code:
while ($row = mysql_fetch_assoc($result)) { //echo "<div>"; //echo "<div class=\"slide-image\">"; print $row['Image']; //echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; //echo "</div>"; //echo "</div>"; }It displays the resulting JPEG as text, which as you might imagine is very difficult to interpret.
最满意答案
您必须使用PHP设置Content-Type标头。 如果它是JPEG,它将是image/jpeg 。 你可以用:
header('Content-Type: image/jpeg');务必在输出任何数据之前设置该标题。
更新
但是在您的文件中,当您从同一文档输出图像数据和文本数据时,您无法将其标题类型设置为多种类型。
一种选择是
while ($row = mysql_fetch_assoc($result)) { echo "<div>"; echo "<div class=\"slide-image\">"; echo "<img src='image_render.php?id=".$row['some_key_for_record']."'"; //print $row['Image']; we are moving it to an external page echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; echo "</div>"; echo "</div>"; }并在image_render.php中
//code to pull data from db according to $_GET['id']; header('Content-Type: image/jpeg'); print $row['Image']; //no text data to be output from hereYou have to set the Content-Type header with PHP. If it is JPEG it will be image/jpeg. You can set it with:
header('Content-Type: image/jpeg');Be sure to set that header before outputting any data.
update
But in your file as you are outputting image data and text data from same document you cant set its header type as multiple types.
one option would be
while ($row = mysql_fetch_assoc($result)) { echo "<div>"; echo "<div class=\"slide-image\">"; echo "<img src='image_render.php?id=".$row['some_key_for_record']."'"; //print $row['Image']; we are moving it to an external page echo "</div>"; echo "<div class=\"slide-text\">"; echo $row['Head']; echo $row['Description']; echo "</div>"; echo "</div>"; }and in image_render.php
//code to pull data from db according to $_GET['id']; header('Content-Type: image/jpeg'); print $row['Image']; //no text data to be output from here更多推荐
发布评论