I am trying to display 4 images using PHP and MySQL database. I have to display the 4 images as rows.
I use the table cars with fields (id_car
, car_image1
, car_image2
, car_image3
, car_image4
), with all the images being blob datatype.
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("cars_database");
$sql = "SELECT car_image1, car_image2, car_image3, car_image4 FROM cars WHERE id_car='$id'";
$result = mysql_query("$sql");
mysql_close($link);
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
echo $row['car_image1'];
echo $row['car_image2'];
echo $row['car_image3'];
echo $row['car_image4'];
}
I can only display 1 image and not the other images. Since I am a newbie to this technology I need help.
That's how HTTP works (at least, current version): you cannot have a URL that points to more than one resource simultaneously. Your script needs to send one image.
Simply, call it four times:
<img src="/get-image.php?id=1">
<img src="/get-image.php?id=2">
<img src="/get-image.php?id=3">
<img src="/get-image.php?id=4">
You need to define path for images.
<?php
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
?>
<img src="path<?php echo $row['car_image1']; ?>" alt="" />
<img src="path<?php echo $row['car_image2']; ?>" alt="" />
<img src="path<?php echo $row['car_image3']; ?>" alt="" />
<img src="path<?php echo $row['car_image4']; ?>" alt="" />
<?php
}
?>