Display Image From Database Php Mysql

10 Answers 10

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'] ).'"/>';                              

answered Jan 28 '16 at 8:53

0

For example if you use this code , you can load image from db (mysql) and display it in php5 ;)

              <?php    $con =mysql_connect("localhost", "root" , "");    $sdb= mysql_select_db("my_database",$con);    $sql = "SELECT * FROM `news` WHERE 1";    $mq = mysql_query($sql) or die ("not working query");    $row = mysql_fetch_array($mq) or die("line 44 not working");    $s=$row['photo'];    echo $row['photo'];     echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:128px;height:128px">';    ?>                          

answered Dec 29 '14 at 22:14

              <?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'].'">';     }     ?>                          

answered Jun 15 '16 at 13:17

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.

answered May 24 '14 at 7:11

put you $image in img tag of html

try this

              echo '<img src="your_path_to_image/'.$image.'" />';                          

instead of

              print $image;                          

your_path_to_image would be absolute path of your image folder like eg: /home/son/public_html/images/ or as your folder structure on server.

Update 2 :

if your image is resides in the same folder where this page file is exists
you can user this

              echo '<img src="'.$image.'" />';                          

answered May 24 '14 at 7:09

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.

answered May 24 '14 at 7:32

Simply replace

                print $image;                              

with

                                  echo '<img src=".$image." >';                              

answered May 24 '14 at 7:11

4

  • Why you use <?php inside an echo?

    May 24 '14 at 7:35

  • ans has been Updated !

    May 24 '14 at 7:37

  • Great. Now it may help question's owner

    May 24 '14 at 7:39

  • Never ever render a filename this way, consider putting it between quotes (double quotes this time). Although specifications allows for missing quotes, one will get in trouble if misses closed tags and his filename contains "/ "

    user1299518

    May 24 '14 at 7:57

                $sqlimage  = "SELECT image FROM userdetail where `id` = $id1";     $imageresult1 = mysqli_query($link, $sqlimage);      while($rows=mysqli_fetch_assoc($imageresult1)) {      echo "<img src = 'Image/".$row['image'].'" />';   }                              

answered Jan 2 '16 at 0:10

1

  • You should usually include an explanation in your answer, not just code. It otherwise may not always going to be clear why it works (or if it works). Also, code-only answers without explanations often get downvoted and deleted.

    Jan 2 '16 at 0:38

                <?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                              

answered Sep 24 '17 at 13:20

1

  • Please edit your post and add explanation to your code.

    Sep 24 '17 at 13:47

put this code to your php page.

              $sql = "SELECT * FROM userdetail"; $result = mysqli_query("connection ", $sql);  while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {     echo "<img src='images/".$row['image']."'>";     echo "<p>".$row['text']. "</p>"; }                          

i hope this is work.

answered May 9 '18 at 13:31

Not the answer you're looking for? Browse other questions tagged php mysql or ask your own question.

Display Image From Database Php Mysql

Source: https://stackoverflow.com/questions/23842268/how-to-display-image-from-database-using-php

0 Response to "Display Image From Database Php Mysql"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel