How to display an BLOB image stored in MySql database?

前端 未结 4 2101
孤城傲影
孤城傲影 2020-11-30 11:56

I am trying to display the last 5 images uploaded to my \"store\" table in MySql. I\'m a complete noob to PHP and databases and i\'ve been reading a lot on how to do this bu

相关标签:
4条回答
  • 2020-11-30 12:24

    This is what I used when I wanted to do something like that... a long time ago! =P

    $sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
    $result = mysqli_query($db,$sql);
    while($arraySomething = mysqli_fetch_array($result))
    {
        echo "<img src='php/imgView.php?imgId=".$arraySomething."' />";
    }
    
    0 讨论(0)
  • 2020-11-30 12:31

    You can also use this function

    //Retrieve image from database and display it on html webpage    
    function displayImageFromDatabase(){    
    //use global keyword to declare conn inside a function    
    global $conn;    
    $sqlselectimageFromDb = "SELECT * FROM `imageuploadphpmysqlblob` ";    
    $dataFromDb = mysqli_query($conn, $sqlselectimageFromDb);    
    while ($row = mysqli_fetch_assoc($dataFromDb)) {    
    echo '<img height="250px" width="250px" src=data:image;base64,'.$row['image'].'/>';    
    }
    

    Insert it into mysql database like this :

    $image = $_FILES['imagefile']['tmp_name'];
    $name = $_FILES['imagefile']['name'];
    $image = base64_encode(file_get_contents(addslashes($image)));
    

    references : https://mauricemutetingundi.blogspot.com/2019/04/how-to-upload-blob-image-to-mysql.html

    0 讨论(0)
  • 2020-11-30 12:38

    I try the first approach with header('content-type: image/jpeg'); but end up with image not shown. After a few google through website I found the solution which I can display image from database to my page

    try this:

    mysql_connect("localhost","root","")or die("Cannot connect to database"); //keep your db name
    mysql_select_db("example_db") or die("Cannot select database");
    $sql = "SELECT * FROM `article` where `id` = 56"; // manipulate id ok 
    $sth = mysql_query($sql);
    $result=mysql_fetch_array($sth);
    // this is code to display 
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/>'
    
    0 讨论(0)
  • 2020-11-30 12:39
    mysql_connect("localhost","root","")or die("Cannot connect to database"); 
    
    //keep your db name
    mysql_select_db("example_db") or die("Cannot select database");
    
    $sql = "SELECT * FROM `article` where `id` = 56"; 
    // manipulate id ok 
    $sth = mysql_query($sql);
    $result=mysql_fetch_array($sth);
    // this is code to display 
    
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/> width="xxxx" height="xxxx"';
    

    Add the height and width also

    0 讨论(0)
提交回复
热议问题