0

I use this following code to display the image from database this code fetch the image in the unreadable format, in database i create the image type in "blob" please help me to display image dynamically when user enter name of the image it should display that image only

my db table table name store

id | name |image
---------------
1 |  xxxx| (image)

<?php
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());

$query = "SELECT * FROM store where fname = 'ss' ";

$info = mysql_query($query) or die(mysql_error());

$num = mysql_num_rows($info);




$sql ="select image from store where fname = 'ss' ";

$result = mysql_query($sql) or die(mysql_error());

if($result){
    echo'
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1   bordercolor="#2696b8">
            <tr>

</tr>';


while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                echo'<tr>


<td align="center" width="150" height="200"><img src="datadesign.php' . $row['image'] . '">


</tr>';
                    }
    echo'</table>';
}

else{
    echo'<h1> System Error </h1> table ';
    exit();
}
mysql_close();
?>
abbas
  • 35
  • 1
  • 8

2 Answers2

0

Try this :

header("Content-type: image/jpeg");
     echo mysql_result($result, 0);
Arun Kumar M
  • 848
  • 9
  • 14
0

Basicly there is 2 possible options. Use Data URI scheme (http://en.wikipedia.org/wiki/Data_URI_scheme) or not. I usually prefer use 2 scripts without data uri. Those are only examples to give you an idea, so you may need to edit code blow.

1. You need 2 scripts to create table with image.

table.php

<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1   bordercolor="#2696b8">
<tr>
<td align="center" width="150" height="200"><img src="image.php?image_id=1">
</tr>
</table>

image.php

mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());
$sql ="select image from store where id= '".intval($_GET['image_id'])."' ";
$result = mysql_query($sql) or die(mysql_error());
header('Content-Type: image/png');
if($result){
  $row = mysql_fetch_row($result);
  echo $row['image'];
  mysql_close();
}
else
{
  echo readfile('/your/path/error/image.png');
}

2. You need 1 script to create table with image.

table.php

mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());

$query = "SELECT * FROM store where fname = 'ss' ";

$info = mysql_query($query) or die(mysql_error());

$num = mysql_num_rows($info);
if ($num > 0)
{
echo'
<table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1   bordercolor="#2696b8">
            <tr>

</tr>';


while($row = mysql_fetch_array($info, MYSQL_ASSOC)){
                echo'<tr>


<td align="center" width="150" height="200"><img src="data:image/png;base64,'.base64_encode($row['image']). '">


</tr>';
                    }
    echo'</table>';
}
 mysql_close();
Aivar
  • 2,029
  • 20
  • 18
  • image not displaying in this table – abbas Aug 22 '13 at 07:14
  • if i have more image in my database table how can i search the particular image from that table and display. – abbas Aug 22 '13 at 08:05
  • @abbas Both cases expects that image is in PNG file format. If not, then image cant be displayed, as they are given with header image/png. If they are some other format you need to specify different header, example you can save mime type to database. For displaying more info about image you can use example echo ''.$row['image'].''; For searching specific image you can use: $sql ="select image from store where name Like '%searching_this_image%' "; – Aivar Aug 22 '13 at 14:09