php中如何从数据库中读取图片?

2025-04-14 14:38:39
推荐回答(1个)
回答1:


//将图片存进数据库再读出,注意存储图片的字段类型必须为blob
$user=’root’;
$password=’root’;
$db=’test’;
$connect=mysql_connect(‘localhost’,$user,$password);
mysql_set_charset(‘utf8′,$connect);
mysql_select_db($db);

$photo = “0x”.bin2hex(file_get_contents(“./test.jpg”));
$sql=”INSERT INTO `test`.`test` (`photo`) VALUES ($photo);”;//$photo不需要用引号,切记
mysql_query($sql);

//$result=mysql_query(“SELECT *
//FROM `test`
//LIMIT 0 , 30〃);
//$img=mysql_fetch_array($result);
//echo $img['photo'];
?>