sk121506 Posted January 22, 2008 Share Posted January 22, 2008 i can upload an image and insert the information i want about it into a database, the problem is, im trying to insert the image itself into the database as a medium blob. im not sure what im doing wrong because when i echo the query and try selecting the image to view it after it has been inserted into the database, a bunch of funny looking symbols appear. is this the binary form of the image? how can i get around this? some of my code is below $content = addslashes(fread(fopen($file,"r"), filesize($filetmpName))); $query = "INSERT INTO $Images (Image_Name, Size, Image, Username, Type, URL) VALUES ('$filename', '$filesize', '$content', '$_SESSION[username]', '$filetype', '$URL')"; $result= MYSQL_QUERY($query); echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/ Share on other sites More sharing options...
pkSML Posted January 22, 2008 Share Posted January 22, 2008 You would call the image as so in your page: < img src="http://yoursite.comimage.php?id=6"> (without the space :-) In your image.php script, you send the image like this: // get the image from the db $sql = "SELECT image FROM testblob WHERE image_id=0"; // the result of the query $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); // set the header for the image header("Content-type: image/jpeg"); echo mysql_result($result, 0); Source: http://www.phpriot.com/articles/images-in-mysql/8 Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-445748 Share on other sites More sharing options...
sk121506 Posted January 22, 2008 Author Share Posted January 22, 2008 still getting lots of the funny looking code symbols instead of the image even after i add that into my code Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-445759 Share on other sites More sharing options...
pkSML Posted January 22, 2008 Share Posted January 22, 2008 Are you sending out a proper header? I don't think you're understanding how to use the image script. You have a webpage that you want to place a database image in. We'll say your script is called image.php. In your webpage, you display the image as < img src="/image.php?id=5"> Then you should see an image like it's supposed to be. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-445774 Share on other sites More sharing options...
sk121506 Posted January 22, 2008 Author Share Posted January 22, 2008 gettin a red x in place of the image now. maybe the problem is im not uploading it right? do i need to change anything to any proper method of uploading an image to a database besides what i have? Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-445789 Share on other sites More sharing options...
pkSML Posted January 22, 2008 Share Posted January 22, 2008 Try comparing what you have to what's in this tutorial: http://www.phpriot.com/articles/images-in-mysql/1 (this tutorial has 10 pages to look through) I'll have more time to look at this later today. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-445965 Share on other sites More sharing options...
sk121506 Posted January 22, 2008 Author Share Posted January 22, 2008 even after that im still gettin the same results maybe its a setting i have to change somewhere in phpmyadmin? Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-446311 Share on other sites More sharing options...
pkSML Posted January 22, 2008 Share Posted January 22, 2008 Can you paste the code to display your image on The Code-Bin and post the link? (I'm thinking the HTTP header has something to do with your problem.) Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-446486 Share on other sites More sharing options...
sk121506 Posted January 23, 2008 Author Share Posted January 23, 2008 http://code-bin.homedns.org/86 Thanks for the help as I'm trying to figure this problem out. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-447104 Share on other sites More sharing options...
pkSML Posted January 24, 2008 Share Posted January 24, 2008 <?php header('Content-type: image/gif'); readfile("image.gif"); //sends binary information -- the image ?> Above is the barebones to displaying an image with PHP. Notice you need to send an HTTP header that tells the browser what kind of information it's receiving. That's why you're getting all the gobbly-gook. The browser doesn't know it's an image. Search image mime types for the correct mime type to use. Notice that all your code is missing is the header. //test viewing an image from the database $query = "SELECT Image, Type FROM $Images WHERE ImageId='11'"; $result= MYSQL_QUERY($query); $data = @ mysql_fetch_array($result); echo $data["Image"]; Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-447428 Share on other sites More sharing options...
sk121506 Posted January 24, 2008 Author Share Posted January 24, 2008 just want to clarify, im only saving the image into the database not my server in case that is what the readfile function is for. regarding the header... if i put it in the middle of my code, it says the header information has already been sent, if i put it at the beginning of my code before everything with single quotes, it says internet explorer cannot open my web page, if i put the header in double quotes, it shows just an x where the image should display and a ÿ (i forgot to add in the codebin under $data... i put $type = @MYSQL_RESULT($result,0, "Type"); to read the type and added $type as the content-type in the header which should be correct. so.... no funny looking symbols.... just a red x where the image should be, any more ideas? again, thanks for stickin with me in this problem i'm having. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-447443 Share on other sites More sharing options...
pkSML Posted January 26, 2008 Share Posted January 26, 2008 OK. This is really not that complex. The uploading is the complex part. Your image.php script: (yes, ten lines is all this simple script needs) <?php $imageid = $_GET['file']; // *****Insert MySQL connection details here***** $query = "SELECT Image, Type FROM $Images WHERE ImageId='$imageid'"; $result= MYSQL_QUERY($query); $data = @ mysql_fetch_array($result); header("Content-type: image/jpeg"); echo $data["Image"]; ?> Your page with the image on it: <img src="http://www.******.com/imageupload.php?file=11"> BTW, what type of image have you uploaded? Is it a JPG, GIF, PNG??? That will affect the header in the image script. I think that since you're getting the red box with the X in it, you should re-upload the image to the database. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-449890 Share on other sites More sharing options...
sk121506 Posted January 29, 2008 Author Share Posted January 29, 2008 for future viewers, i made the mistake of not taking the source of the image from another page... the whole time i was trying to integrate everything into one page... make an image.php and imageupload.php and you'll be set. i wanna thank pkSML for helping me through this the entire time. PHP Freaks..... recommend it to anyone..... GREAT forum. Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-451883 Share on other sites More sharing options...
pkSML Posted January 29, 2008 Share Posted January 29, 2008 Glad to hear it's finally working for ya'! Quote Link to comment https://forums.phpfreaks.com/topic/87148-solved-trying-to-insert-image-to-database/#findComment-451896 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.