How to display image from database using php

前端 未结 11 2655
闹比i
闹比i 2020-11-27 21:43

I am trying to display an image coming from the database and I was not able to display the image .but its showing like this user-1.jpg Please see my code can on

相关标签:
11条回答
  • 2020-11-27 22:19

    Displaying an image from MySql Db.

    $db = mysqli_connect("localhost","root","","DbName"); 
    $sql = "SELECT * FROM products WHERE id = $id";
    $sth = $db->query($sql);
    $result=mysqli_fetch_array($sth);
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
    
    0 讨论(0)
  • 2020-11-27 22:24

    instead of print $image; you should go for print "<img src=<?$image;?>>"

    and note that $image should contain the path of your image.

    So, If you are only storing the name of your image in database then instead of that you have to store the full path of your image in the database like /root/user/Documents/image.jpeg.

    0 讨论(0)
  • 2020-11-27 22:25
    <?php
        $conn = mysql_connect ("localhost:3306","root","");
        $db = mysql_select_db ("database_name", $conn);
    
        if(!$db) {
            echo mysql_error();
        }
    
        $q = "SELECT image FROM table_name where id=4";
        $r = mysql_query ("$q",$conn);
        if($r) {
             while($row = mysql_fetch_array($r)) {
                header ("Content-type: image/jpeg");       
        echo $row ["image"];
            }
        }else{
            echo mysql_error();
        }
        ?>
    
    sometimes problem may  occures because of port number of mysql server is incoreect to avoid it just write port number with host name like this "localhost:3306" 
    in case if you have installed two mysql servers on same system then write port according to that
    
    in order to display any data from database please make sure following steps
    1.proper connection with sql
    2.select database
    3.write query 
    4.write correct table name inside the query
    5.and last is traverse through data
    
    0 讨论(0)
  • 2020-11-27 22:29

    You need to do this to display image

    $sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
    $imageresult1 = mysql_query($sqlimage);
    
    while($rows=mysql_fetch_assoc($imageresult1))
    {
        $image = $rows['image'];
        echo "<img src='$image' >";
        echo "<br>";
    } 
    

    You need to use html img tag.

    0 讨论(0)
  • 2020-11-27 22:35
    <?php
           $connection =mysql_connect("localhost", "root" , "");
           $sqlimage = "SELECT * FROM userdetail where `id` = '".$id1."'";
          $imageresult1 = mysql_query($sqlimage,$connection);
    
          while($rows = mysql_fetch_assoc($imageresult1))
        {       
           echo'<img height="300" width="300" src="data:image;base64,'.$rows['image'].'">';
        }
        ?>
    
    0 讨论(0)
提交回复
热议问题