dmcke5 Posted May 20, 2009 Share Posted May 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/ Share on other sites More sharing options...
Brian W Posted May 20, 2009 Share Posted May 20, 2009 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/#findComment-838263 Share on other sites More sharing options...
dmcke5 Posted May 21, 2009 Author Share Posted May 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/#findComment-838560 Share on other sites More sharing options...
dmcke5 Posted May 21, 2009 Author Share Posted May 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/#findComment-838882 Share on other sites More sharing options...
Dathremar Posted May 21, 2009 Share Posted May 21, 2009 Use absolute path to the image ie: http://yourdomain/path/imagename.extension Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/#findComment-838887 Share on other sites More sharing options...
dmcke5 Posted May 21, 2009 Author Share Posted May 21, 2009 I dont think that answers my question Dathremar?... The file is stored in a mysql database as binary information, Not as a file in the file system. Or am I missing something here? Quote Link to comment https://forums.phpfreaks.com/topic/158931-retrieving-image-data-from-a-mysql-database/#findComment-838890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.