我想在php上显示图像,它作为blob类型存储在mysql数据库中

时间:2011-12-11 23:21:18

标签: php mysql

我想在mysql数据库中显示php上的图像。我使用以下代码:

 $last_id = mysql_insert_id();
           echo "Image Uploaded. <p /> Your image <p /> <img src=get.php?id=$last_id>";
<?php
 mysql_connect("localhost","root","") or die(mysql_error());

 mysql_select_db("image") or die(mysql_error());

 $id = $_REQUEST['id'];

 $result = mysql_query("SELECT * FROM image WHERE id = $id");

 $image = $result['image'];

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

1 个答案:

答案 0 :(得分:3)

首先,Don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial


试试这个:

<?php
$db=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image",$db) or die(mysql_error());

$id = (int)$_REQUEST['id'];

$result = mysql_query("SELECT * FROM image WHERE id=$id",$db);
if(mysql_numrows($result)>=1){
    $row = mysql_fetch_assoc($result);
    header("Content-type: image/jpeg");
    echo $row['image'];
}
?>

同时

 echo "Image Uploaded. <p /> Your image <p /> <img src=get.php?id=$last_id>";

无效的HTML。

 echo 'Image Uploaded. <p> Your image </p> <img src="get.php?id='.$last_id.'" />';