【问题标题】:Image is broken when retrieved as blob image from mysql database从 mysql 数据库中检索为 blob 图像时图像损坏
【发布时间】: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>

【问题讨论】:

标签: php html mysql image


【解决方案1】:

改变这个

<img src="image.php? id=<?php echo $row["id"]; ?>" />

<img src="image.php?id=<?php echo $row['id']; ?>"/>

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 2012-08-20
    • 2015-07-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多