KBBach Posted October 24, 2009 Share Posted October 24, 2009 Hi! I'm trying to store images in the largeblob format on my sql server (wamp-server) and then displaying them on a PHP site, but pretty much nomatter what I try it seems that php vomits.. I've tried different image (jpg/jpeg) files but I think phpmyadmin screws them up or something.. Anyway... here's the code: <head> <title>Med-Tech.dk<title> <link rel="stylesheet" href="css/style.css" type="text/css"> </head> <body> <html> <table class="main"> <tr> <td class="banner" height="120px"> <div id="banner"> </div> </td> </tr> <tr> <td> <div id="links"> <a href="index.php">Home</a> | <a href="products.php">Products</a> | <a href="contact.php">Contact</a> </div> </td> </tr> <tr> <td> <div id="bodytext"> <?php $con = mysql_connect("localhost","reader","reader"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("medtech", $con); $result = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($result)) { echo '<table class="products">'; echo '<tr>'; echo '<td colspan=2>'; echo '<h1>'; echo $row['Name']; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td width=200>'; #echo 'billede her'; header('Content-Type: image/jpeg'); echo $row['PIC']; echo '</td>'; echo '<td>'; echo $row['SDesc']; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan=2>'; echo '<h3>'; echo $row['ArticleNR'] ; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan=2>'; echo $row['Desc']; echo '</td>'; echo '</tr>'; echo '</table>'; } mysql_close($con); ?> </div> </td> </tr> </table> </html> </body> Any wise words would be appreciated - I've been working on this all day and my sanity is balancing on the edge of a knife... Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2009 Share Posted October 24, 2009 You cannot output image data inside of HTML. Also, you cannot output a content-type header or any other type of header after content has been output to the browser. You must use a HTML <img tag to place an image on a web page inside of HTML - http://w3schools.com/html/html_images.asp The URL that goes into the <img src="URL of an image" alt=""> must be to a .php file that outputs the correct Content-type header followed by the correct image data. Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943543 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 Wise words # 1 - Turn on error reporting (error_reporting(E_ALL)) at the beginning of ALL scripts. You cannot send a header in the middle of a page, and you can't dump image data to the browser without the header. (Well, you can, but it looks like crap). I don't know any way to do this in one pass. If anyone does, I'd be interested to see it. To accomplish this, make that an IMG tag that will run a script to dump the image data: echo '<td width=200>'; #echo 'billede her'; //** REMOVE THESE TWO LINES AND ADD THE ONE BELOW // header('Content-Type: image/jpeg'); // echo $row['PIC']; echo '<IMG src=getProdImg.php?id=' . $row['ID'] . '>'; // id=# or whatever your unique identifier is echo '</td>'; Then you write getProdImg.php to send the header(), retrieve the image data from the database and send it to the browser. Do not send any HTML from this script, it is only sending an image. Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943551 Share on other sites More sharing options...
KBBach Posted October 24, 2009 Author Share Posted October 24, 2009 Sooo.... what I need to do is to make a page that does nothing but getting the image from the sequel server and then link to that page? Errhmm how do I carry over the ID (unique identifier) from the products.php to the getProdImg.php to get the right image? Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943553 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 Yes, you need to write a new script that sends an image to the browser. Usually, when you want an image, you use <IMG src="ImageFile.jpg"> The browser then sends a request to the server for it to send "ImageFile.jpg" and the browser puts the image data where the IMG tag is. Since the request for the image file is a standard GET request, we can use a script to send an image <IMG src="getImage.php?file=ImageFile.jpg"> then write the script to display the image header('Content-Type: image/jpeg'); $data = file_get_contents($_GET['file']); echo $data; [ Of course there's no error checking or security checking there, this is just an example ] This script can do database access; or anything else you want. However, the output can ONLY be image data. That's why my previous post suggested echo '<IMG src="getProdImg.php?id=' . $row['ID'] . '">'; // id=# or whatever your unique identifier is I don't know the layout of your table, or what your primary key is called; so I suggested 'ID' which is fairly common. You need to write getProdImg.php to retrieve the image data from the database using the ID (or whatever you pass in as the identifier) and output the header and the data. Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943565 Share on other sites More sharing options...
KBBach Posted October 24, 2009 Author Share Posted October 24, 2009 Hum well... I can see the logic.. But I'm working on the getProdImage and can't get it working... getting following error: Parse error: parse error in C:\wamp\www\MedTech\getProdImg.php on line 3 Well here's my code: <?php error_reporting(E_ALL) $con = mysql_connect("localhost","reader","reader"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("medtech", $con); if(isset($_GET['id'])) { $id=$_GET['id']; $img = mysql_query("select PIC from products where ID="+$id); echo $img; } ?> Anyway.. .The Databse ID is simply called ID and the largeblob with the image is called PIC... Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943573 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 A couple of minor things: You are missing the '.' in the query to combine the two strings @mysql_query("select PIC from products where ID=" . $id); After mysql_query, you still have to fetch the row: $res = @mysql_query("select PIC from products where ID="$id); $img = mysql_fetch_array($res); echo $img[0]; And a couple of side notes: Since the output from this script is going to an image tag, it can be difficult to see any error messages or problems. If you just get a broken image placeholder on your page, you can request this script by typing it in the browser's address bar ( getProdImg.php?id=2 ), and then you will either see the image if it works, or an error message or something. For debugging, you can echo stuff out - which will break the image tag, but let you see what's happening. Just take out all the debug echos in the final script. There is nothing magical about this script. It can be requested from the browser (as I just suggested). So you need to sanitize the input to prevent SQL injection attacks, etc. Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943579 Share on other sites More sharing options...
KBBach Posted October 24, 2009 Author Share Posted October 24, 2009 So now it's working ^^ Thanks you both so much for your help!!!! Just to summerize : Products.php <head> <title>Med-Tech.dk<title> <link rel="stylesheet" href="css/style.css" type="text/css"> </head> <body> <html> <table class="main"> <tr> <td class="banner" height="120px"> <div id="banner"> </div> </td> </tr> <tr> <td> <div id="links"> <a href="index.php">Home</a> | <a href="products.php">Products</a> | <a href="contact.php">Contact</a> </div> </td> </tr> <tr> <td> <div id="bodytext"> <?php $con = mysql_connect("localhost","reader","reader"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("medtech", $con); $result = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($result)) { echo '<table class="products">'; echo '<tr>'; echo '<td colspan=2 class="leftrightborder">'; echo '<h1>'; echo $row['Name']; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td width=200 class="leftborder">'; ##echo 'billede her'; echo '<IMG width=200 src=getProdImg.php?id=' . $row['ID'] . '>'; echo '</td>'; echo '<td class="rightborder">'; echo $row['SDesc']; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan=2 class="leftrightborder">'; echo '<h3>'; echo $row['ArticleNR'] ; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan=2 class="leftrightbotborder">'; echo $row['Desc']; echo '</td>'; echo '</tr>'; echo '<tr>'; echo '<td class="leftrightborder" colspan=2>'; echo ' '; echo '</td>'; echo '</tr>'; echo '</table>'; } mysql_close($con); ?> </div> </td> </tr> </table> </html> </body> getProgImg.php: <?php ##error_reporting(E_ALL) $con = mysql_connect("localhost","reader","reader"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("medtech", $con); if(isset($_GET['id'])) { $id=$_GET['id']; $res = mysql_query("select PIC from products where ID=" . $id); $img = mysql_fetch_array($res); header('Content-Type: image/jpeg'); echo $img[0]; } ?> This is for a school project, so I really wanna credit your guys! You can send me some info on whom to credit on a pm Quote Link to comment https://forums.phpfreaks.com/topic/178850-solved-largeblob-image-from-mysql-to-website-corrupted/#findComment-943587 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.