Jump to content

Retrieving Image Data From a mysql Database


dmcke5

Recommended Posts

Hey guys im having trouble getting the data i need out of my mysql table and i was hoping someone could help :)

Its an assignment for uni so im using some of the lecturers code, and just modifying it(so i apologize in advance for it being terribly messy). Anyway, the site is an online auction site. I have 4 rows for the images(the last 4 in the table) in my mysql table: imagedata(BLOB), imagename(Varchar(40)), imagesize(Varchar(40)), imagetype(Varchar(40)). All of which seem to receive the appropriate data when the file is submitted from the form. The problem is with viewing them, Here is the code i am using:

$sql = mysql_query("SELECT * FROM items2 WHERE description LIKE '%$_POST[keywords]%' AND categoryid = $_POST[categories] OR name LIKE '%$_POST[keywords]%' AND categoryid = $_POST[categories]");	//Search database for keywords
	while ($row = mysql_fetch_array($sql, MYSQL_NUM)) {
			$min = $row[4] + 1;

			$output .= ("<hr><p> Item: <a href='item.php?uniqueid=$row[5]'>$row[0]</a> <br> Seller: $row[3] <br> Price: $$row[4]
					<br>Image: <img src='getImage.php?id=$row[5]' $row[8] alt='$row[7]'></p>");
					}
		$end = ("</div>");

 

Here is getImage.php:

 

<?php
include'/export/student/s2681238/mysql/mysqlconfig.php';
include'/export/student/s2681238/mysql/mysqlconnect.php';
$id = $_GET['id'];

  $query = "select imagedata, imagename, imagetype, imagesize " .
           "from items2 where uniqueid = $id";
  $result = @ mysql_query($query) or showerror();
  $row = mysql_fetch_array($result);
  $image = $row;

$data = $image['imagedata'];
$name = $image['imagename'];
$type = $image['imagetype'];
$size = strlen($data);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");

?>

 

The Data that is stored in the table is as follows:

 

imagedata: blob 21.5kb

imagename: cap1.jpg

imagesize: width="200" height="194"

imagetype: image/jpeg

 

The Code results in an empty white box the size of the image i'm attempting to display. Ive got absolutely no idea whats wrong with it :-[

 

Anyway its about 2Am here in Australia So im going to bed, hopefully ill have some good ideas by the morning :)

 

Cheers, Daniel

Link to comment
Share on other sites

Assuming that the image is being stored in the same folder as the file getImage.php, this is what you need I think...

<?php
include'/export/student/s2681238/mysql/mysqlconfig.php';
include'/export/student/s2681238/mysql/mysqlconnect.php';
$id = intval($_GET['id']);//added intval() to prevent SQL injections... 

  $query = "select imagedata, imagename, imagetype, imagesize " .
           "from items2 where uniqueid = $id";
  $result = @ mysql_query($query) or showerror();
  $row = mysql_fetch_array($result);
  $image = $row;

$data = $image['imagedata'];
$name = $image['imagename'];
$type = $image['imagetype'];
$size = strlen($data);//This is getting the count of characters in the name of the file, not the file...
$size = filesize($data);//this gets the file size

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");

echo file_get_contents($data);//get the image's code and print it out
?>

Link to comment
Share on other sites

Ok well i tried that and it hasn't solved the problem yet... Im still presented with a white Box with the dimensions of the correct picture. When You right click and select properties this is what it gives me(incase it helps):

 

Image Properties:

Location: Http://myhost.com/mypicture

Type: JPEG image

Image Dimensions: 0px × 0px (scaled to 200px × 194px)

Size of file: 0 KB (3 bytes)

Alternative Text: cap1.jpg

 

It wouldn't Actually be putting the image into the database wrong would it? As in somehow corrupting the data?

 

I'm not sure what you meant by "assuming its stored in the same folder"

either, as i thought it was stored in the database? If you can clarify that would be great :)

 

Cheers, Daniel

 

Link to comment
Share on other sites

Ok well after more playing with it i've got it displaying the right image size too ie:

Size of file: 21.5 KB

 

But the confusing thing is it still says Image Dimensions: 0px x 0px...

I showed it too my lecturer today and he looked at it for and hour and a half and couldn't work it out.

Please help, its Due on monday :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.