radiations3 Posted August 3, 2011 Share Posted August 3, 2011 I have uploaded my images perfectly in the blob field but i am not able to display the image i used the following code but its not working: NEED HELP <?php $username = "root"; $password = ""; $host = "localhost"; $database = "bakery"; @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); @mysql_select_db($database) or die("Can not select the database: ".mysql_error()); $id = 3 ; if($id==3){ $query = mysql_query("SELECT img FROM pic WHERE Desc='".$id."'"); $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/ Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 HELP!!! Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251336 Share on other sites More sharing options...
JasonLewis Posted August 3, 2011 Share Posted August 3, 2011 "It's not working" is not a good way to describe a problem. Some more details would be nice. Are you getting a corrupt image error or something? Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251338 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 "It's not working" is not a good way to describe a problem. Some more details would be nice. Are you getting a corrupt image error or something? I am not getting any (corrupt or required) image and its only displaying the following message if i comment the header... Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\DBMS\get.php on line 6 MY ACTUAL CODE IS FOLLOWING: <?php require_once('Connections/hammad.php'); ?> <?php mysql_select_db("bakery") or die(mysql_error()); $id=3; $image = mysql_query("SELECT img FROM pic WHERE Desc='".$id."'"); $image = mysql_fetch_assoc ($image); $image = $image ['$image']; header("content-type: image/jpg"); echo $image; ?> Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251342 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 After putting or die(mysql_error()); on my select query its displaying following error message please help: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Desc='3'' at line 1 $image = mysql_query("SELECT img FROM pic WHERE Desc='".$id."'") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251345 Share on other sites More sharing options...
JasonLewis Posted August 3, 2011 Share Posted August 3, 2011 You can't use the word 'desc' in your queries, unless you backtick it. 'desc' is a reserved word for MySQL, so you can order things. SELECT * FROM table ORDER BY id DESC I'd suggest choosing a different column name or surrounding it with backticks: WHERE `Desc`='" . $id . "' Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251349 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 You can't use the word 'desc' in your queries, unless you backtick it. 'desc' is a reserved word for MySQL, so you can order things. SELECT * FROM table ORDER BY id DESC I'd suggest choosing a different column name or surrounding it with backticks: WHERE `Desc`='" . $id . "' Yeah that was a problem but now its displaying following error message: Notice: Undefined index: $image in D:\DBMS\get.php on line 7 <?php require_once('Connections/hammad.php'); ?> <?php mysql_select_db("bakery") or die(mysql_error()); $id=3; $image = mysql_query("SELECT img FROM pic WHERE `Desc`='".$id."'") or die(mysql_error()); $image = mysql_fetch_assoc ($image); $image = $image ['$image']; //header("content-type: image/jpg"); echo $image; ?> KIndly let me know am i processing the right code for displaying an image from blob??? Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251354 Share on other sites More sharing options...
JasonLewis Posted August 3, 2011 Share Posted August 3, 2011 It looks like you're getting muddled up. In your query you specify: SELECT img FROM pic By that logic, the blob column is called img So this line: $image = $image['$image']; Makes absolutely no sense because 1) you're referencing the $image variable inside single quotes which is making it into a string and 2) your column is titled img not $image Change that line to: $image = $image['img']; Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251357 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 It looks like you're getting muddled up. In your query you specify: SELECT img FROM pic By that logic, the blob column is called img So this line: $image = $image['$image']; Makes absolutely no sense because 1) you're referencing the $image variable inside single quotes which is making it into a string and 2) your column is titled img not $image Change that line to: $image = $image['img']; Now i am able to display image but only the rubbish (corrupt) content of the image is getting displayed..... I don't know why is it happening I was expecting a JPEG image you really helped out to make my code absolutely perfect and thanks for that but i guess there is a problem with some logic of displaying the image... Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251363 Share on other sites More sharing options...
JasonLewis Posted August 3, 2011 Share Posted August 3, 2011 Ensure there is no whitespace before and after the opening/closing PHP tags, also try using image/jpeg as your content type. See what happens there... I've never stored images in a database before so I wish you luck. Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251366 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 Ensure there is no whitespace before and after the opening/closing PHP tags, also try using image/jpeg as your content type. See what happens there... I've never stored images in a database before so I wish you luck. YAAAAAAAAAAAHHHHHHHHHOOOOOOOOOOOOO my problem of uploading the image in database is solved now thanks alot Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251367 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 Final Without any error code <?php require_once('Connections/hammad.php'); ?> <?php mysql_select_db("bakery") or die(mysql_error()); $id=addslashes(3); $image = mysql_query("SELECT img FROM pic WHERE `Desc`='".$id."'") or die(mysql_error()); $image = mysql_fetch_assoc ($image); $image = $image ['img']; header("content-type: image/jpg"); echo $image; ?> Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251368 Share on other sites More sharing options...
JasonLewis Posted August 3, 2011 Share Posted August 3, 2011 No worries, just a question. Was it because of whitespace? Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251372 Share on other sites More sharing options...
radiations3 Posted August 3, 2011 Author Share Posted August 3, 2011 Yes it was!! Quote Link to comment https://forums.phpfreaks.com/topic/243646-how-to-display-image-from-blob-field/#findComment-1251379 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.