我一直试图从数据库中显示图像,但它只显示不寻常的字体代替图像。我试过这段代码:
$db = new mysqli("localhost", "root", "", "learndb");
$stmt=$db->prepare("SELECT * FROM studentrecords WHERE id=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$result=$stmt->get_result();
$myrow = $result->fetch_assoc();
echo '<img src="'.$myrow["image-data"].'" >';
答案 0 :(得分:2)
您的解决方案位于this link here,您应该在其中添加:
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
进入你的代码:
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM studentrecords WHERE id=$id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
所以在你的情况下:
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM studentrecords WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $myrow['image-data'] ).'"/>'
并且不再使用MySQL,您可以使用MySQLi或PDO。