I have written the following code to retrieve image from mysql database:

<?php
include('dbinfo.inc.php');
include('../functions.php');
mysql_connect("localhost","username","password")
or die("Failure to communicate");
mysql_select_db("db")
or die("Could not connect to Database");

$query = "SELECT content,size,type FROM upload WHERE id=1;";
$result=mysql_query($query);
mysql_close();

$content=mysql_result($result,0,"content");
$size=mysql_result($result,0,"size");
$type=mysql_result($result,0,"type");

header('Content-Type: '.$type);
print $content;
?>

It is working.. Now I would like to retrieve multiple images at a time..
what shall I do?
please help.. its urgent

You can retrieve only one image at a time.

Try to make this code as a function.
I'll give you an example:

<?php
include('dbinfo.inc.php');
include('../functions.php');
mysql_connect("localhost","username","password")
or die("Failure to communicate");

mysql_select_db("db")
or die("Could not connect to Database");

function displayimage($id)
{
$query = "SELECT content,size,type FROM upload WHERE id='$id';";//$id is the id of your pic or whatever...
$result=mysql_query($query);
mysql_close();

$content=mysql_result($result,0,"content");
$size=mysql_result($result,0,"size");
$type=mysql_result($result,0,"type");

header('Content-Type: '.$type);
imagejpeg($content); //I have changed "print $content;" to this one...
}
$one=1;
$two=2;
$three=3;

//just enter in the parameter of your function whatever id you want..
$one1=displayimage($one);
$two2=displayimage($two);
$three3=displayimage($three);

echo '<img src="'.$one1.'">'; 
echo '<img src="'.$two2.'">';
echo '<img src="'.$three3.'">';

?>

I think that's it.
3,2,1...ZERO.

Member Avatar for fatihpiristine

use fetch_array command with while circle.

Thanks ryan_vietnow for replying..

Basically I have a database where each row has three images..

And each row has an id, i.e. all three images have only one id..

so I am confused about the part in your code

$one=1;$two=2;$three=3;

what shall I do to make the code run for me.

Please Help

Member Avatar for fatihpiristine

i will send you the code that i write soon. it support up to hundreds of images on one row...

Member Avatar for fatihpiristine

<?php
$Get = $_GET;
$GetID = "select * from dbtable where ID='" . $Get . "'";
$Result = mysql_query($GetID);
$NumberofPicture = 3 // Define here have many columns stores your pictures data.

while($Write = mysql_fetch_array($Result))
{
$ID = $Write;

for($i = 1; $i < $NumberofPictures; $i++)
{
$Picture[] = $Write; // Name of column will appear like Picture1, Picture2
}
}
?>


<?php
// to print the data out from array
echo $Picture[1];
?>

i could not find the exact code i wanted to give you but i hope this would help you.

Thanks fatihpiristine for replying.

can you please explain me the line

$Get = $_GET;

what is NewsID meant for?

Member Avatar for fatihpiristine

ahaa :) i forgot to change. it is belong to mine

NewsID stands for the row id, requests and assing the data to $Get which is posted

Try to make this code as a function.
I'll give you an example:

<?php
include('dbinfo.inc.php');
include('../functions.php');
mysql_connect("localhost","username","password")
or die("Failure to communicate");

mysql_select_db("db")
or die("Could not connect to Database");

function displayimage($id)
{
$query = "SELECT content,size,type FROM upload WHERE id='$id';";//$id is the id of your pic or whatever...
$result=mysql_query($query);
mysql_close();

$content=mysql_result($result,0,"content");
$size=mysql_result($result,0,"size");
$type=mysql_result($result,0,"type");

header('Content-Type: '.$type);
imagejpeg($content); //I have changed "print $content;" to this one...
}
$one=1;
$two=2;
$three=3;

//just enter in the parameter of your function whatever id you want..
$one1=displayimage($one);
$two2=displayimage($two);
$three3=displayimage($three);

echo '<img src="'.$one1.'">'; 
echo '<img src="'.$two2.'">';
echo '<img src="'.$three3.'">';

?>

I think that's it.
3,2,1...ZERO.

thanks ryan_vietnow.
I have tried your code but it is not working.
Is there any other way to solve the problem?

ahaa :) i forgot to change. it is belong to mine

NewsID stands for the row id, requests and assing the data to $Get which is posted

Thank you BUT
Still its not working..

Is that so?I have tested it and it is working on mine.
Anyway,try to seperate the function to another page and include it,but do not use it as a function,make it just as an include page.Wish I could help you....

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.