how to retrieve and output images from multiple columns with php and mysql?
UPDATE:
I have have ten columns in my SQL table: id, imgid, urlid, image1, image2, image3, image4, image5, and comment. Id, imgid, and urlid are int type. Image[1-5] are mediumblob type. Url and comment are text type. Imgid is the number of images uploaded (which should be 5), urlid is the number of urls submi开发者_运维技巧tted (which should be one right now), url holds the url, and comment holds user comments. I'd like to retrieve and output all the image data from the image columns in a row but I'm getting torn page icons. I am using a front.php to output images and a get.php to convert images to viewable format.
Here's my front.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$defaultqry = mysql_query ("SELECT * FROM dbp");
while($row = mysql_fetch_assoc($defaultqry))
{
echo ($row['image1'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=1 " width="30" height="30"/> ' : '';
echo ($row['image2'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=2 " width="30" height="30"/> ' : '';
echo ($row['image3'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=3 " width="30" height="30"/> ' : '';
echo ($row['image4'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=4 " width="30" height="30"/> ' : '';
echo ($row['image5'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=5 " width="30" height="30"/> ' : '';
}
?>
Here's my get.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$query = mysql_query("SELECT * FROM dbp WHERE id = ".mysql_real_escape_string($_GET['id']));
$row = mysql_fetch_array($query);
$col = intval($_GET['col']);
if(isset($row['image'.$col]) && $row['iamge'.$col] != NULL){
$content = $row['image'.$col];
}else{
exit; // col is not existent, or image is empty
}
header('Content-type: image/jpg');
echo $content;
exit;
?>
first, you use a DATAbase, store data in it not files. (you should store the link/path to the files in the DB not the images itself, this only blows up your database and makes it slow) see Storing Images in DB - Yea or Nay? for more info on this topic.
but if you want to do it this way then you will have to output the id of the row AND a column identifier like:
echo ($row['image1'] != NULL) ? '<img src="get.php?id='.$row['id'].'&col=1 " width="30" height="30"/> ' : '';
in your get.php:
mysql_connect ("localhost","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$query = mysql_query("SELECT * FROM dbp WHERE id = ".mysql_real_escape_string($_GET['id']));
$row = mysql_fetch_array($query);
$col = intval($_GET['col']);
if(isset($row['image'.$col]) && $row['image'.$col] != NULL){
$content = $row['image'.$col];
}else{
exit; // col is not existent, or image is empty
}
header('Content-type: image/jpg');
echo $content;
exit;
精彩评论