2

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 submitted (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;




?>
user701510
  • 5,563
  • 17
  • 61
  • 86
  • 1
    Why would you want to store the images in BLOB field? Rather store it in the file system and map their path to the db fields. This would make it easier to manage the image files and reduce your db calls. – Sujit Agarwal Jun 02 '11 at 04:57
  • I was just wondering how I could do it... – user701510 Jun 02 '11 at 05:04

1 Answers1

1

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;
Community
  • 1
  • 1
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • so I just need to make 4 more lines of code like "echo ($row['image1'] != NULL) ? '" and then change the get.php and I'm set? – user701510 Jun 02 '11 at 06:34
  • nope, is there some code I need to put in to check error messages? – user701510 Jun 03 '11 at 06:21
  • just call the get.php with the right parameters directly, so you see any error. e.g get.php?id=1&col=1 – Rufinus Jun 03 '11 at 09:26
  • Calling get.php directly I get this error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/coroom5/public_html/michael/DBP/get.php on line 8 – user701510 Jun 03 '11 at 23:28
  • What do you mean if the id column is right or if I called it differently? Its called "id" and I put in exactly what you typed. There is only one row of data in the table. I neglected to mention in my OP that I have four more columns in the table but I'm not calling them so I don't think it matters. I'll go update my OP – user701510 Jun 05 '11 at 03:07
  • LOL, u spelled "image" wrong here: $row['iamge'.$col] != NULL) It works perfertly now. Thanks, bro! – user701510 Jun 05 '11 at 03:47
  • when you are pleased with the answer, why you didnt mark it as right answer ? – Rufinus Jun 05 '11 at 09:20