View Full Version : showing images stored in MySQL with same id
rayzun
05-21-2005, 06:27 AM
Has anyone got an idea on how to show multiple images stored in a table, the images have a field which I use to reference them from my webpage
'modelid'
Except after planning the webpage and upload form, I now need to display the image/s, and I use modelid to query the database
id modelid name type size content
42 11 P10000222.jpg image/jpeg 58618 [BLOB - 57.2 KB]
44 11 P1000022.jpg image/jpeg 514344 [BLOB - 502.3 KB]
The display script I have will only show one image:
$query = "SELECT name, type, size, content " .
"FROM upload WHERE modelid = '$id'
LIMIT 0 , 3";
$result = mysql_query($query);
$number = mysql_numrows($result);
{
for ($i=0; $i<$number; $i++) {
$content=mysql_result($result,$i,"content");
$size=mysql_result($result,$i,"size");
$type=mysql_result($result,$i,"type");
echo $content;
To get the image:
print "<img src=\"/formshow.php?id=$id\" width=\"55\">";
Tangerine Dream
05-21-2005, 10:15 PM
Hi, probably you should remove "LIMIT 0 , 3" and add outer FOR loop to output all images:
$query1 = "SELECT id FROM upload";
$result1 = mysql_query($query1);
$number1 = mysql_numrows($result1);
// if your image id starts at 1
for( $i=1; $i<$number; $i++ ) {
// ** begin your code **
$query = "SELECT name, type, size, content " . ....
echo $content;
// ** end your code **
}
rayzun
05-22-2005, 10:04 AM
No results will display with the above query script.
The table does contain more than one entry (line) per the field 'modelid'.
Thanks for trying
NancyJ
05-22-2005, 01:51 PM
ofcourse it will only show one image... you only use one <img src... etc
why are you using a separate file for this? i'm assuming there is a good reason so I will continue with that in a separate file... this should work
$query = "SELECT name, type, size, content " .
"FROM upload WHERE modelid = '$id'
LIMIT 0 , 3";
$result = mysql_query($query);
$number = mysql_numrows($result);
{
for ($i=0; $i<$number; $i++) {
$content=mysql_result($result,$i,"content");
//are these 2 used for anything?
$size=mysql_result($result,$i,"size");
$type=mysql_result($result,$i,"type");
echo "<img src = \"$content\">";
To get the images:
include 'formshow.php?id=$id';
fwiw I think this thread would have more success in the php forum, theres nothing wrong with your SQL query
You should be querying the database for all ids with modelid=11 in the main page.
Then pass that id to the formshow.php?id=$id calls within seperate img tags.
Finally amend the sql in formshow to be based up id rather than modelid.
Notes: echoing binary to page within an img tag would need some subtle encoding rather than just output and is limited to about 2kb per img tag - thus a remote script is needed that just outputs the data.
To be proper you should really output the correct headers based on mimetype and filesize - for those browsers that do not force imagetype headers to img src output.
That is yet another reason why it is easier and quicker to just store the image and a pointer rather than put the thing in a database as a long/med/blob
rayzun
05-25-2005, 12:59 AM
Hi
______
just store the image and a pointer rather than ...
______
I have a form script that uploads everything to mysql, it works fine, all text fields and image get uploaded to a table. I would like to change the script to allow for your suggestion and send the image to the filesystem and keep the path and all other fields in mysql. I have not been able to find a example of a form which does the basics.
If you could have a look at the script and forward the way in which to make the change...
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
<form name="formCheck" action="" method="post" enctype="multipart/form-data" ...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.