developr Posted October 29, 2009 Share Posted October 29, 2009 Ok, so first off would like to say Hello So I've been trying to figure out how to store images in a mysql database, and as far as i can tell the images are stored but getting them out seems to be the problem. when i try to go to the page on my webhost it says that "Can not select the database: Access denied for user 'testimg'@'localhost' to database 'testImages'" and when i goto show.php?id=1 on my local mamp install it gives me all kinds of weird symbols ÿÀ�™Ì�ÿÄ�È���������������������������!1AQaq"2‘¡±BR#Ábr‚’Ñ¢Â3CS$á²Òsƒ“%ðñc£Ã4â³T56Dt”E&Ód¤´ÄUu•7���!1AQaqð‘¡"2±ÁÑáñBRÂbr‚¢â#3ÿÚ���?�Ü/é -è i've checked everything in the code a million times and searched google and this forum for anything that can help me but i haven't been able to find something that has helped me understand what exactly is going on and why heres the upload form <form enctype="multipart/form-data" action="insert.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="1500000" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <input value="Submit" type="submit"> </form> this is my insert.php that processes the image after its submitted <?php include './database.php'; $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db ($database); if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { $tmpName = $_FILES['image']['tmp_name']; $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); $query = "INSERT INTO tbl_images "; $query .= "(image) VALUES ('$data')"; $results = mysql_query($query, $link); print "Thank you, your file has been uploaded."; }else{ print "No image selected/uploaded"; } mysql_close($link); ?> and heres the show.php that displays the image using the id <?php include './database.php'; @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 = $_GET['id']; if(!isset($id) || empty($id)){ die("Please select your image!"); }else{ $query = mysql_query("SELECT image FROM tbl_images WHERE id='".$id."'"); $row = mysql_fetch_assoc($query); $content = $row['image']; header("Content-type: image/jpg"); echo $content; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179439-retrieving-images-from-mysql-database-using-php/ Share on other sites More sharing options...
DavidAM Posted October 29, 2009 Share Posted October 29, 2009 First, use the code tags, which is not the typewriter, it's the # button. Yeah, I know, it's not intuitive. Took me a while to find it, but it is worth the effort. Since you used addslashes() on the data before you inserted it into the database, you are going to have to stripslashes() to remove them before you echo the data. I would recommend using mysql_real_escape() instead (you don't have to undo that when you retrieve the data). But if you already have data in the database, you can't switch without updating all existing data. As for the "Access denied" you are either using the wrong user, password or database; or you do not have access to the database. You'll have to talk to your webhost about that. Quote Link to comment https://forums.phpfreaks.com/topic/179439-retrieving-images-from-mysql-database-using-php/#findComment-946815 Share on other sites More sharing options...
lakshmiyb Posted October 29, 2009 Share Posted October 29, 2009 just go through this it may help u <?php $number_of_thumbs_in_row = 4; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename,photo_category FROM gallery_photos"); while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/><br>$row[1]<br>$row[3]<br>$row[0]"; } mysql_free_result( $result ); $result_final = "<tr valign='top' align='center' class='style1'>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</tr align='center' class='style1'>\n<tr align='center' class='style1'>\n"; } else $counter++; $result_final .= "\n<td class='style1'>".$thumbnail_link."</td>\n"; } if($counter) { if($number_of_photos_in_row==$counter) $result_final .= "\n<td class='style1' colspan='".($number_of_photos_in_row=$counter)."'></td>\n"; $result_final .= "</tr>"; } } echo <<<__HTML_END <html> <head> <title>Gallery View</title> </head> <body> <table width='100%' border='0' cellpadding="10" cellspacing="10"> $result_final </table> </body> </html> __HTML_END; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179439-retrieving-images-from-mysql-database-using-php/#findComment-946940 Share on other sites More sharing options...
developr Posted November 2, 2009 Author Share Posted November 2, 2009 Thanks for the response, DavidM and lakshmiyb I tried stripslashes() with existing and new entires into the database and mysql_real_escape() with new ones but neither worked I'll keep trying to get it working, thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/179439-retrieving-images-from-mysql-database-using-php/#findComment-949143 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 If your server has magic quotes enabled, then it could be adding slashes into your code, thus causing things to be double quotes so that is always worth testing. You can check by checking your active php.ini, using phpinfo() or get_magic_quotes_gpc() to find out if it's enabled. There is no reason to really use addslashes when working with a MySQL database. You should instead use mysql_real_escape_string as this will successfully insert the data without slashes appearing in the actual database thus corrupting your data. Finally why store the image in a database, personally I'd recommend storing it on the webserver and store only a path to it in the database. Quote Link to comment https://forums.phpfreaks.com/topic/179439-retrieving-images-from-mysql-database-using-php/#findComment-949342 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.