Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display image from database using php [duplicate]

Tags:

php

mysql

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;
}
like image 875
Xavi Avatar asked May 24 '14 07:05

Xavi


People also ask

How display all images from database in php?

php to display the images from database. We will use MySQL select query to retrieve the image in database. Copy the below code and paste it in between <div class="img-box"> ... </div> of show.

How fetch image from database in php and display in form?

Check whether the user selects an image file to upload. Retrieve the content of image file by the tmp_name using PHP file_get_contents() function. Insert the binary content of the image in the database using PHP and MySQL. Show the image uploading status to the user.

How fetch image from database in php and display in Div?

php $id = $_GET['id']; // do some validation here to ensure id is safe $link = mysql_connect("localhost", "root", ""); mysql_select_db("dvddb"); $sql = "SELECT imageColumn FROM Tablename WHERE id=$id"; $result = mysql_query("$sql"); $row = mysql_fetch_assoc($result); mysql_close($link); header("Content-type: image/jpeg ...


2 Answers

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'] ).'"/>';
like image 51
Charles Avatar answered Oct 13 '22 06:10

Charles


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">';
   ?>
like image 35
Yo Ne S Avatar answered Oct 13 '22 06:10

Yo Ne S