-1

Display all the images using loop from DB.this is my code. Image is saved to database as bite format. can't retrieve and display all the images using loop. for example img1,img2,img3 etc...Anyone know what i might be doing wrong?

$username = "root";
$password = "123";
$host = "localhost";
$database = "test";
@mysql_connect($host, $username, $password) or die("Can not connect to database:      ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT * FROM tbl_images");
while($row = mysql_fetch_array($query))
{
header('Content-type: image/jpg');
$content = $row['image'];
echo $content;
}
arun
  • 323
  • 6
  • 17
  • 3
    You can't serve multiple images in this way. Once you send a header, only one image can be outputted. Sending a second header and a second output after that is not possible. See for example [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – Boaz Mar 16 '13 at 09:01
  • 1
    Just... what do you imagine the result would look like...?! – deceze Mar 16 '13 at 09:05
  • @Boaz thanks for your vital info. then how can i display all the images. i used to store like this method $tmpName = $_FILES['image']['tmp_name']; $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); $query = "INSERT INTO tbl_images "; $query .= "(image) VALUES ('$data')"; $results = mysql_query($query, $link); – arun Mar 16 '13 at 09:07
  • @arun do you want to view all the images in one page? – Adi Mar 16 '13 at 09:12
  • @deceze:thanks for your reply.i want look like images in list view img1,img2,img3 etc.. – arun Mar 16 '13 at 09:12
  • @arun You can create one script to output the HTML markup with `img` tags, and another script to serve a single image like in your example. See for example [How to display all the images stored inside a database](http://stackoverflow.com/questions/8758548/how-to-display-all-the-images-stored-inside-a-database) – Boaz Mar 16 '13 at 09:14
  • Then you need to make an HTML page with `` elements, each of which links to *one* individual image which you can output this way. – deceze Mar 16 '13 at 09:14
  • @Adnan yes.i want to display all images like a list view in a page. – arun Mar 16 '13 at 09:15
  • @Boaz actually i store images in file format. how can i fetch the source for tag? if i done this means the output will be showing like a encrypted format. – arun Mar 16 '13 at 09:20
  • @arun You can also try Adnan's answer. – Boaz Mar 16 '13 at 09:22
  • @deceze i have stored the images in a file format.if i used tag the output will be in encrypted format.its not displaying the images. – arun Mar 16 '13 at 09:25
  • @Boaz yeah i m going to try it. – arun Mar 16 '13 at 09:30

2 Answers2

2

You can output the images in HTML format by doing something like this

while($row = mysql_fetch_array($query))
{
   echo '<img src="data:image/jpg;base64,'.base64_encode($row['image']).'" /><br />';
}

Since you'll be fetching the images from the database in binary, you can simply encode them in Base64 and send them to the browser

Adi
  • 5,089
  • 6
  • 33
  • 47
0

Use this simple method,

while($row = mysql_fetch_array($query))
{
$content = $row['image'];
echo "<img src='path/.$content'>";
}
Arif
  • 1,222
  • 6
  • 29
  • 60
  • Would mind explaining why you think this would solve the problem? – Adi Mar 16 '13 at 09:34
  • why it wouldn't work ? – Arif Mar 16 '13 at 09:38
  • Because the OP is storing the images in binary in the database. He doesn't have an actual path containing the images. But for the future, you're the one writing the answer, so you're the one who should prove his/her claim. – Adi Mar 16 '13 at 09:41
  • if you are storing images with their full path in DB then you just need to fetch that field other wise you have to give the path or simply define as constant value – Arif Mar 16 '13 at 09:49