Reputation: 33
php
include("connect_to_database.php");
if( !isset($_FILES["image"]) ){
echo "upload the file";
}else{
$image = mysql_query("SELECT * FROM upload WHERE id=1");
$image = mysql_fetch_assoc($image);
$image = $image["image"];
echo $image;
}
html
<form action="newindex.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" value="Upload" >
</form>
Above code displaying the corresponding id's image which is already uploaded in mysql in BLOB format.
Now, I need to display images of last 6 id's or all the images in database. I have tried the below code but its not working instead it's displaying 1 image.
php
include("connect_to_database.php");<br>
$query = mysql_query("SELECT * FROM upload ORDER BY id DESC");
while( $rows = mysql_fetch_array($query) ){
$image = $rows["image"];
header("Content-type: image/jpeg");
echo $image."<br/>";
}
Upvotes: 0
Views: 973
Reputation: 416
You shouldn't output multiple images into same response. The web browser does not expect/understand another image after the first one, just as copying two jpg files into same file won't work on desktop.
Instead, create another (dynamic) page containing all the links to the images (<img src="img.php?id=3">
). The web browser will then request the images one at a time.
$id = (int)$_REQUEST['id'];
$query = mysql_query("SELECT * FROM upload WHERE id=$id");
(Please note code similar to above is really insecure, one should never use input values directly in SQL queries in any public environment. I'd also recommend using PDO instead for executing the queries).
Upvotes: 2