vmicchia Posted February 17, 2011 Share Posted February 17, 2011 So I am trying to display an image from a database (I know it's not the best idea but it's what I was told to do). Anyway I'm having problems displaying it. Here is the code for displaying the image. $cat = $_GET['cat']; include 'includes/openDbConn.php'; $sql = 'SELECT * FROM CushionsCategories WHERE CushionCategory = "'.$cat.'"'; echo $sql; $result = mysql_query($sql); echo '<table>'; while($val = mysql_fetch_assoc($result)){ $cush = 'SELECT * FROM Cushion WHERE SKU = "'.$val['CushionSKU'].'"'; echo $cush; $cushres = mysql_query($cush); $cushval = mysql_fetch_assoc; $img = 'SELECT * FROM Images WHERE SKU = "'.$val['CushionSKU'].'"'; echo $img; $imgres = mysql_query($img); $imgval = mysql_fetch_assoc($imgres); if( $i % 3 == 0 ) { echo '</tr><tr>'; } echo '<td><img src="image.php?sku='.$val['CushionSKU'].'" name="'.$imgval['FileName'].'" description="'.$imgval['Description'].'" /></td>'; } echo '</tr></table>'; and the page that gets the image: $sql = 'SELECT Image FROM Images WHERE SKU = "'.$sku.'"'; //echo $sql; $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); Thanks for any help in advance. Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/ Share on other sites More sharing options...
chrispos Posted February 17, 2011 Share Posted February 17, 2011 Just put the image name in the database ie beach.jpg under a table called image run the query and do the image source as echo "<img src='$image'>"; Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1175795 Share on other sites More sharing options...
BlueSkyIS Posted February 17, 2011 Share Posted February 17, 2011 Just put the image name in the database ie beach.jpg under a table called image run the query and do the image source as echo "<img src='$image'>"; already stated that image is stored in the database and needs to pull image from database and send directly to browser, thus no possibility for a simple image link to a file. Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1175799 Share on other sites More sharing options...
vmicchia Posted February 18, 2011 Author Share Posted February 18, 2011 Yes unfortunately it's in the database I for some reason can't get it to display with that code( I was following a tutorial) and was hoping for some help with that. Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1176295 Share on other sites More sharing options...
denno020 Posted February 18, 2011 Share Posted February 18, 2011 Give this link a go mate: http://www.codewalkers.com/c/a/Database-Articles/Storing-Images-in-Database/3/ It was easy enough to find using Google... Denno Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1176304 Share on other sites More sharing options...
vmicchia Posted February 21, 2011 Author Share Posted February 21, 2011 I had looked at this link before, it seemed a lot like the tutorial I was using. Anyway I tried it the way this shows. I changed the code to how this tutorial shows and it is still not working for me. When I go to the image.php it echos out Array like I assume it should but on the page the image is displayed I still get a broken link. here is what the code looks like now: $sql = 'SELECT Image FROM Images WHERE SKU = "'.$sku.'"'; //echo $sql; $result = mysql_query($sql); if (mysql_num_rows($result) != 0) { $row = @mysql_fetch_array($result); $image_type = $row["Type"]; $image = $row["Image"]; header ("Content-type: $image_type"); print $image; } Edit: Also when I do a print_r on the results it still only says array rather than giving me the contents of the array. Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1177621 Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2011 Share Posted February 21, 2011 What's your actual code in image.php, because so far you are not setting the $sku variable to anything and your query won't match anything. Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1177627 Share on other sites More sharing options...
vmicchia Posted February 21, 2011 Author Share Posted February 21, 2011 Here's the actual code. I got it figured out though. I was just sending a variable wrong. $sku = $_GET['sku']; //echo $sku; include 'includes/openDbConn.php'; $sql = 'SELECT * FROM Images WHERE SKU = "'.$sku.'"'; //echo $sql; $result = mysql_query($sql); if (mysql_num_rows($result) != 0) { $row = mysql_fetch_assoc($result); $image_type = $row["Type"]; //echo $image_type; $image = $row["Image"]; // echo $image; header ("Content-type: $image_type"); echo $image; } Quote Link to comment https://forums.phpfreaks.com/topic/228017-help-with-displaying-images/#findComment-1177629 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.