如何使用blob数据类型从数据库显示图像?

时间:2015-03-20 12:19:36

标签: php mysqli

我正在尝试在表'用户'列'image'下的数据库'cemembers'中显示jpg图像。有了这个逻辑,我创建了自定义代码,但是当我在php中运行我的页面时,只回显存储在数据库中但没有图形视图的文件名。例如'cdda_dd.jpg'我稍后检查了浏览器中的图像,我可以看到问题可能是编码错误的图像src ...请查看我的代码并帮助我。

 <?php 

    //setting connection variables
    $hostname = "localhost";
    $username= "root";
    $password ="";
    $db= "cemembers";

    //connect to database
    $mysqli_db = new mysqli( $hostname, $username, $password, $db);

    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }

    $sql = mysqli_query($mysqli_db, "SELECT * FROM users WHERE image  ");
    $row= mysqli_fetch_array($sql);
    $photo= $row['image'];
    echo $photo;
?>

1 个答案:

答案 0 :(得分:0)

使用此代码..修改在第18行&amp; 21.从列名称中选择图片&#34;图像&#34; id = 1或任何内容而不是select * from users ..

<?php
    //setting connection variables
    $hostname = "localhost";
    $username= "root";
    $password ="";
    $db= "cemembers";
    //connect to database
    $mysqli_db = new mysqli( $hostname, $username, $password, $db);

    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }

        $sql = mysqli_query($mysqli_db, "SELECT image FROM users WHERE id=1  ");
        $row= mysqli_fetch_array($sql);
        $photo= $row['image'];
        header("Content-type:image/jpeg"); // add this
        echo $photo;

?>