tchick Posted March 29, 2011 Share Posted March 29, 2011 Hi all, Newbie here, i am having a problem to get my images to show which are stored in mysql database as a mediumblob. I get id number to print in table ut am just getting empty square with red cross in where my image should be. Is my code incorrect or is it something else? Appreciate your help with this. I have included both of the pages codes i am using. Thanks Tony image2.php <?php include("common.php"); error_reporting(E_ALL); $link = mysql_connect(host,username,password) or die("Could not connect: " . mysql_error()); mysql_select_db(db) or die(mysql_error()); $sql = "SELECT id FROM photos"; $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); ?> <table border="1"><tr><td>id</td><td>image</td></tr> <?php while($row=mysql_fetch_assoc($result)){ print '<tr><td>'.$row['id'].'</td><td>'; print '<img src="image1.php?id='.$row['id'].'height="75" width="100"">'; } echo '</td></tr></table>' ?> image1.php <?php ob_start(); include("common.php"); mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); $query = mysql_query("SELECT imgage FROM photos WHERE id={$_GET['image_id']}"; $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/ Share on other sites More sharing options...
KevinM1 Posted March 29, 2011 Share Posted March 29, 2011 In image1.php, you're attempting to echo the image data itself, but in image2.php you're trying to use that as the path of the image source. Raw image data is not the same as the image's path. You also have syntax errors in several places, including using the wrong value for your GET in image1.php. It should simply be $_GET['id']. That said, just so I'm completely clear, fixing those syntax errors won't fix your main problem. I'm not sure why you don't simply use a function to do what you want to do. It's simpler/cleaner than having a separate script. Try something like the following (all in one file - not tested): function displayImage($id){ $query = mysql_query("SELECT imgage FROM photos WHERE id = $id"); $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } include("common.php"); error_reporting(E_ALL); $link = mysql_connect(host,username,password) or die("Could not connect: " . mysql_error()); mysql_select_db(db) or die(mysql_error()); $sql = "SELECT id FROM photos"; $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); ?> <table border="1"><tr><td>id</td><td>image</td></tr> <?php while($row=mysql_fetch_assoc($result)){ echo '<tr><td>'.$row['id'].'</td><td>'; displayImage($row['id']); } echo '</td></tr></table>'; At the very least, that's the approach you should be using. Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1193792 Share on other sites More sharing options...
tchick Posted March 30, 2011 Author Share Posted March 30, 2011 Hi Nightslyr, Thanks for the reply, errors taken on board, I have tried your function script which as you rightly say is the way forward. I have run the script you supplied (untested) and it results in a file save/download box popping up ?. Thanks Tony Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194162 Share on other sites More sharing options...
PFMaBiSmAd Posted March 30, 2011 Share Posted March 30, 2011 That's because you cannot output an image directly in the HTML on a web page, primarily because you cannot output a header() in a HTTP request after you have sent any content to the browser. You must use an HTML <img> tag, because it is the browser that requests the image and renders it on the page. Your original concept was correct, but it has some errors in it. In your image2.php code, you have a mis-placed " in your <img> tag. Use the following line of code - print '<img src="image1.php?id='.$row['id'].'" height="75" width="100">'; Your image2.php code has several problems - 1) You would need to use $_GET['id'] as that is the name of the GET parameter you are putting on the end of the url ?id=123 2) You need to both cast $_GET['id'] as an integer and test that $_GET['id'] has a number in it to prevent sql injection and to prevent sql errors in your query. 3) You apparently have typo in the column name in your query. imgage should be image 4) You should store the image type image/jpg in a column in your table so that your code will support other image types. You would retrieve that value when you retrieve the image data and output it in the Content-type header(). Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194232 Share on other sites More sharing options...
tchick Posted March 30, 2011 Author Share Posted March 30, 2011 Hi PFMaBiSmAd, Thanks for your reply, I have corrected the typo errors (sorry) not sure what you mean though regarding the $_GET issues points 1&2. I will add column to insert the image type into.I have now corrected the typos and included the corrected code below, but still i am getting the id number colum showing ok but the image is still a blank square with a red cross inside?.Is it the $_GET that is causing this ? I know when i check the properties of the images it shows image1.php?id=10 etc. so i think this looks ok?. Thanks Tony image2.php <?php include("common.php"); error_reporting(E_ALL); $link = mysql_connect(host,username,password) or die("Could not connect: " . mysql_error()); mysql_select_db(db) or die(mysql_error()); $sql = "SELECT id FROM photos"; $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); ?> <table border="1"><tr><td>id</td><td>image</td></tr> <?php while($row=mysql_fetch_assoc($result)){ print '<tr><td>'.$row['id'].'</td><td>'; print '<img src="image1.php?id='.$row['id'].'" height="75" width="100">'; } echo '</td></tr></table>' ?> image1.php <?php ob_start(); include("common.php"); mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); $query = mysql_query("SELECT image FROM photos WHERE id=$_GET['id']"; $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; } ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194277 Share on other sites More sharing options...
litebearer Posted March 30, 2011 Share Posted March 30, 2011 You haven't closed the parans here... $query = mysql_query("SELECT image FROM photos WHERE id=$_GET['id']"; Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194301 Share on other sites More sharing options...
PFMaBiSmAd Posted March 30, 2011 Share Posted March 30, 2011 And, your image1.php code contains an extra } that would be producing a fatal php parse error and no output at all. Remove that stray } at the third line from the end of the file. Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194305 Share on other sites More sharing options...
tchick Posted March 30, 2011 Author Share Posted March 30, 2011 Hi PFMaBiSmAd & litebearer Thanks for your replies, I should stop doing several things at the same time, these errors were just my typos (sorry again!)even with these typos corrected i am still getting the id number colum showing ok but the image is still a blank square with a red cross inside?. Thanks Tony image2.php <?php include("common.php"); error_reporting(E_ALL); $link = mysql_connect(host,username,password) or die("Could not connect: " . mysql_error()); mysql_select_db(db) or die(mysql_error()); $sql = "SELECT id FROM photos"; $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); ?> <table border="1"><tr><td>id</td><td>image</td></tr> <?php while($row=mysql_fetch_assoc($result)){ print '<tr><td>'.$row['id'].'</td><td>'; print '<img src="image1.php?id='.$row['id'].'" height="75" width="100">'; } echo '</td></tr></table>' ?> image1.php <?php ob_start(); include("common.php"); mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); $query = mysql_query("SELECT image FROM photos WHERE id=$_GET['id']"); $row = mysql_fetch_array($query); $content = $row['image']; header('Content-type: image/jpg'); echo $content; ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194322 Share on other sites More sharing options...
PFMaBiSmAd Posted March 30, 2011 Share Posted March 30, 2011 Your (current) image1.php code will still be producing a fatal php parse error, due to missing {} around the $_GET['id'] in the query (you had these in your original code.) $query = mysql_query("SELECT image FROM photos WHERE id={$_GET['id']}"); Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194326 Share on other sites More sharing options...
tchick Posted March 30, 2011 Author Share Posted March 30, 2011 Hi PFMaBiSmAd, Fantastic!!!, it finally works thank you so much for your help with this, I really appreaciate you helping me solve this. Regards Tony Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1194332 Share on other sites More sharing options...
jcg Posted May 22, 2012 Share Posted May 22, 2012 Hi all, I followed all the steps in this topic, but still getting empty square with red cross. I did not include the code, because it is the same as this topic. I've tried everything... other ways, other foruns, ... I'm getting crazy!!!!! will not have to do with the apache server? if an image is from the / images folder, okay ... but an image from the database ... always gives empty square with red cross... Appreciate your help. Thanks JCG Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1347655 Share on other sites More sharing options...
Pikachu2000 Posted May 23, 2012 Share Posted May 23, 2012 Start your own new thread, and post your own code in it, within . . . tags. This one is over a year old. Locked. Link to comment https://forums.phpfreaks.com/topic/232081-unable-to-display-my-images/#findComment-1347833 Share on other sites More sharing options...
Recommended Posts