从 mysql 数据库中检索为 blob 图像时图像损坏
Posted
技术标签:
【中文标题】从 mysql 数据库中检索为 blob 图像时图像损坏【英文标题】:Image is broken when retrieved as blob image from mysql database 【发布时间】:2015-06-09 05:14:40 【问题描述】:我每次都得到损坏的图像。请帮我解决这个问题,我是 php 新手。 imagedata 是我的表中名为属性的 blob 类型的列,我在文件 print.php 中的调用函数如下所示
/*This is image.php file*/
$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "nevin";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
die("Connection failed: " . mysqli_connect_error());
if (!isset($_GET['id']))
//If not isset -> set with dumy value
$_GET['id'] = "undefine";
$id=mysql_real_escape_string($_GET['id']);
$image = mysql_query("SELECT * FROM property where id = '$id'");
if($image)
$image=mysql_fetch_assoc($image);
$image=$image['imagedata'];/*imagedata is column of blob type in my table named property*/
header('Content-Type: image/jpg');/* in my database i have images in jpg format and i am storing images as blob datatype*/
echo $image;
?>
<img src="image.php?id=<?php echo $row["id"]; ?>
/*this is my calling function in file print.php as shown below*/
/*print.php*/
<body>
<?php
$servername = "localhost";
$username = "root";
$password = " ";
$dbname = "Nevin";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
die("Connection failed: " . mysqli_connect_error());
$sql = "SELECT * FROM property";
$result = mysqli_query($conn, $sql);
?>
<section>
<div class="features">
<div class="l-container">
<div class="content-wrap clearfix align-center">
<?php
while($row = mysqli_fetch_assoc($result))
echo '<div class="grid">';?>
<img src="image.php? id=<?php echo $row["id"]; ?>" /><?php
echo '<h2>'.$row["id"].'</h2>'.
'<p>'. $row["city"]. " " . $row["price"].'</p>'.
'</div>';
?>
</div>
</div>
</div>
</section>
</body>
【问题讨论】:
查看此链接:-***.com/questions/25209913/… 【参考方案1】:改变这个
<img src="image.php? id=<?php echo $row["id"]; ?>" />
到
<img src="image.php?id=<?php echo $row['id']; ?>"/>
【讨论】:
以上是关于从 mysql 数据库中检索为 blob 图像时图像损坏的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 PHP 从 MYSQL 中插入和检索图像 blob?
错误 java.sql.SQLException:从数据库中检索 Blob(图像)时 SQLite JDBC 驱动程序未实现