Hi, I am trying to display two or more images from a database and I cannot get them to display simultaneously.

I have a database table called 'tbl_images' which has two fields 'id' and 'image' here is the code for the database creation:

CREATE TABLE tbl_images (
id tinyint(3) unsigned NOT NULL auto_increment,
image blob NOT NULL,
PRIMARY KEY (id)
);

Images are successfully stored as a BLOB within the table.

Here is my php code to display the images, the code will successfully display the first image in the database, but will not then go on to display the second image in my database. I can display the second image if I choose to display it through its ID so all images are correctly stored.

<?php
header('Content-type: image/jpg');

include_once 'tables.php';

$query = "SELECT * FROM tbl_images";
$result = mysql_query($query);

for ($j=0; $j<$rows; ++$j)
{
    $row = mysql_fetch_row($result);
    echo $row[1] . "<br /><br /><br /><br /><br /><br /><br /><br /><br />";
}

//below is a second method that I tried which had the same result

/*
while($r=mysql_fetch_array($query))
{
header("Content-type: image/jpeg");
print $r['image'];
}
*/

?>

I have done some research into trying to fix this before posting here and I read a post in another forum which mentioned that you need to link to the php page which processes the displaying of images from a separate php file which actually displays the images. The post did not exlain how to do this and there were no more comments about it so I do not know if that is a correct solution.

I will be very grateful is anyone can help, Thanks.

Recommended Answers

All 7 Replies

You can only display one image using this technique. Put the loop in a calling script. This is done with the image tag:

<?php
// ...
$query = "SELECT * FROM tbl_images";
$result = mysql_query($query);

foreach ($result as $image) { ?>
<img src="image.php?id=<? echo $image['id'];?>" />
<? } ?>

Your code above (which I call as image.php) could retrieve the appropriate image blob by id (just one) and echo it out. You are echoing an image ( header('Content-type: image/jpg'); ), not html in the above script, so remove the html and only have it echo out one blob.

I am unsure if I have done this correctly but I have put the code:

foreach ($result as $image) { ?>
<img src="image.php?id=<? echo $image['id'];?>" />
<? } ?>

into my existing file which now looks like this:

<?php
header('Content-type: image/jpg');

include_once 'tables.php';

$query = "SELECT * FROM tbl_images";
$result = mysql_query($query);
//$rows = mysql_num_rows($result);
//echo $rows;

for ($j=0; $j<$rows; ++$j)
{
    $row = mysql_fetch_row($result);
    echo $row[1] . "<br /><br /><br /><br /><br /><br /><br /><br /><br />";
//    echo "<img src='$row[1]' />";
}

foreach ($result as $image) { ?>
<img src="show.php?id=<?php echo $image['id'];?>" />
<?php }
?>

I do not get any errors but the image will no longer display, sorry if I am being a bit dumb here, I am new to php.

Thanks for your help.

No. The image tag and loop should be in another file. I just made up the name image.php for the url. You may need to change that.

As I said before, the code that returns an image can only return one image.

The flow is something like this.
1. File one, call it image.php, simply retrieves one image blob and echoes it with appropriate content type (as you do on line 2.) This will be called from an image tag in another html or php file. (This scripts pretends to be an image.) In this script query for the image sent to you by $_GET. Make sure you receive a number by sanitizing it so nobody hacks your database.

2. A view file, call it gallery.php for example, handles the looping and generates html for the view. This is basically what I wrote in my previous post. It contains image tag(s) that call image.php with an appropriate id number for which image it wants.

Ok so my situation now is I have a file called "show.php" which I use to get the images from the database, here is its code:

<?php
header('Content-type: image/jpg');

include_once 'tables.php';

$query = mysql_query("SELECT * FROM tbl_images WHERE id='1'");
//$row = mysql_fetch_array($query);
$rows = mysql_num_rows($query);
//echo $rows;

for ($j=0; $j<$rows; ++$j)
{
    $row = mysql_fetch_row($query);
    echo $row[1] . "<br /><br /><br /><br /><br /><br /><br /><br /><br />";
    echo "<img src='$row[1]' />";
}
?>

And now have a second file which is called "display.php" and using the code you have provided I need to use the $_GET function to display the images. So I get the theory behind it but I am struggling to code it, I have not used the $_GET function before, I understand that it gets a variable from the url, but I don't know if I am supposed to somehow put the variable into the url from "show.php". Here is the code for display.php

<?php
include_once 'tables.php';

$id = $_GET['id'];

foreach ($result as $image) { ?>
<img alt="" src="show.php?id=<?php echo $image['id'];?>" />
<?php }

?>

which when I run display.php I get the error code:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\photo\display.php on line 6

Hope you can help, Thanks.

Becoz Browser don't underStand binary Code of Blob Image .
So we can add Header() to each file so browser can easy know this is blob file data.

This is completed Solution

The main php file where u have to do coding.

while ($row = mysql_fetch_object($result)) {
    echo '<img src="show_image.php?id=' . $row->ID . '">';
}

image.php

$result = mysql_query('SELECT `Picture`, `Type` FROM `db` WHERE `ID` = ' . (int)$_GET['id']);
$row = mysql_fetch_object($result);
header('Content-type: image/' . $row->Type);
echo $row->Picture;

Many more thanks to DaniWeb.
Nice, Wonderful.

Main file code is

<?php
$host = "localhost";
$usr = "root";
$pwd = "";
$db= "db";
conOpen($host,$usr,$pwd,$db);
function conOpen($host,$usr,$pwd,$db){
$con=mysql_connect($host,$usr,$pwd) or die ("Connection Failed ");
mysql_select_db($db,$con);}
$row = mysql_fetch_object(mysql_query("SELECT * FROM newsimage order by nId"));
 echo '<img src="imgShow.php?id=' . $row->nId . '">';
?>

Image file code is

<?php
include("inc/dbd.php"); 
echo $_GET['id'];
$result = mysql_query('SELECT `nImage` FROM `newsimage` WHERE `nId` = ' . (int)$_GET['id']) ;
$row = mysql_fetch_object($result);
header('Content-type: image/jpg');
print $row->nImage;
?>

But not working in my code...
Please help me out of this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.