12

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 one guide me how to display the image.

$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows = mysql_fetch_assoc($imageresult1))
{       
    $image = $rows['image'];    
    print $image;
}
enkrates
  • 626
  • 1
  • 6
  • 17
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • 2
    Tip: please dont use mysql_* functions because they are deprecated. You can use mysqli or PDO. – HddnTHA May 24 '14 at 07:33
  • 3
    Use [Prepared Statements](http://www.php.net/manual/en/pdo.prepared-statements.php) to avoid those annoying SQL injections. Your question is not clear if you have an image in your database or only a file name. – Markus Malkusch May 24 '14 at 07:57
  • @markust i my databse its showing user-1.jpg,can you tell me how to save the image inside the databse – Xavi May 24 '14 at 08:05
  • 1
    @Xavi So your database stores only the file name. This is one valid method for storing files. You got answers already. Storing an image in the database is a different question. Use google if you're interested in answers. – Markus Malkusch May 24 '14 at 08:10

10 Answers10

21

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'] ).'"/>';
Charles
  • 320
  • 2
  • 8
4

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">';
   ?>
Yo Ne S
  • 41
  • 2
2
<?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'].'">';
    }
    ?>
Lizwi
  • 51
  • 6
0

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.'" />';
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

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.

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
0

Simply replace

print $image;

with

 echo '<img src=".$image." >';
Mehar
  • 2,158
  • 3
  • 23
  • 46
0

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


} 
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • 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. – sideshowbarker Jan 02 '16 at 00:38
0
<?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
JHM16
  • 649
  • 8
  • 12
0

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.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156