adi123 Posted August 13, 2010 Share Posted August 13, 2010 Can some please help. I have a mysql database. In the table I have added a colum for pictures as VARCHAR and inserted the locations of the pictures. The pictures are in a seperate folder. I am trying to display the images in a php file but can't see where I am going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/ Share on other sites More sharing options...
hcdarkmage Posted August 13, 2010 Share Posted August 13, 2010 Some code would help us figure out where you are going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1098905 Share on other sites More sharing options...
adi123 Posted August 13, 2010 Author Share Posted August 13, 2010 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 pics FROM tbl_images WHERE id='".$id."'"); $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } Code may need to be rewritten Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099045 Share on other sites More sharing options...
Pikachu2000 Posted August 13, 2010 Share Posted August 13, 2010 echo "<img src=\"$content\">"; Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099049 Share on other sites More sharing options...
Alex Posted August 13, 2010 Share Posted August 13, 2010 There's a few things wrong with that script. First off, when posting code please use [php] or [code] tags; I've edited them in for you this time. Setting $id and then checking if it's set right after is pointless, that will always be true. The advantage of isset is that you can check if a variable is set without it throwing an error message. If you want to directly output the image to the browser then you must output the contents of the image, not the path to the image. You code can be rewritten: 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()); if(!isset($_GET['id'] || empty($_GET['id']){ die("Please select your image!"); }else{ $query = mysql_query("SELECT pics FROM tbl_images WHERE id='".$_GET['id']."'"); $row = mysql_fetch_array($query); readfile($row['image']); header('Content-type: image/jpg'); } I would also suggest that your code more fault safe and secure by 1. escaping the input and 2. Making sure that a row was found before you attempt to use one. Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099050 Share on other sites More sharing options...
adi123 Posted August 14, 2010 Author Share Posted August 14, 2010 If anyone please knows how to write a tutorial or script, that would help a lot. All i need to do is connect to the mysql database and take the location of the images from the database. The image files are stored in a folder called pics. I am trying to display the images in a web page. Can anyone please provide help on how to do this from start to finish. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099174 Share on other sites More sharing options...
Alex Posted August 14, 2010 Share Posted August 14, 2010 Did you even try what I posted? It should work as long as the path is correct... 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()); if(!isset($_GET['id'] || empty($_GET['id']){ die("Please select your image!"); }else{ $query = mysql_query("SELECT pics FROM tbl_images WHERE id='".$_GET['id']."'"); $row = mysql_fetch_array($query); readfile("pics/" . $row['image']); header('Content-type: image/jpeg'); } If you're uploading images with formats other than jpeg you'll need to adjust the header accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099288 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.