1

i am relatively new to php. I have a problem with displaying images in my browser ( google chrome) after retrieving blob data from mysql database.

Basically the following code works when the slashes are added in front of the echo at the bottom. However i have followed other online tutorials and the tutor has been able to display their images without the use of slashes whilst i am unable to get the image up. I just wondered what the standard rule is? Another thing to add - when i do fail to get the images up in the browser i get instead a ting thumbnail. I would really appreciate if anybody could tell me how to reliably display images. The site i wish to create is just about images. So its kind of fundamental. Thanks a lot in advance for your time.

<?php $conn = mysql_connect ("localhost","root","arctic123"); $db = mysql_select_db ("user_images", $conn); if(!$db) { echo mysql_error(); } $q = "SELECT * FROM images"; $r = mysql_query ("$q",$conn); if($r) { while($row = mysql_fetch_array($r)) { //echo $row ['username']; //echo "<br>"; header ("Content-type: image/jpeg"); echo $row ['logo']; //echo "<br>"; } }else{ echo mysql_error(); } ?> 
7
  • For the love of God, don't use mysql_*. Those functions are deprecated and you should really be using mysqli at a minimum... Commented Nov 26, 2012 at 23:12
  • Possible duplicates: stackoverflow.com/questions/5932249/… and stackoverflow.com/questions/5525830/…; Also, besides switching to mysqli, you should probably set up a non-root user for your DB as well. Commented Nov 26, 2012 at 23:14
  • If your site is like "all about images" then why store them in a database? Is there any reason for that? I don't get what "the use of slashes" means. I don't see any slashes but those indicating comments. Commented Nov 26, 2012 at 23:17
  • Since you're new to php I assume you're new to web programming. Try to avoid storing images in a database. Just use the file system. I have encountered only a couple instances where storing images in a database made any sense. Commented Nov 26, 2012 at 23:23
  • possible duplicates of How to read images from MySQL database using PHP? and How can I display images stored in a MySQL database? Commented Nov 26, 2012 at 23:39

3 Answers 3

2

You can't have header after any output in your code: http://php.net/manual/en/function.header.php

Best practice is to upload images to a directory and just store the image's path/file name in the database. Also makes it easier to manipulate the image, e.g. create different sizes and thumbnails with PHP. And it takes about four times less the disk space...

Sign up to request clarification or add additional context in comments.

2 Comments

Can images be sorted in a directory? I had presumed this was impossible. I don't mean manually. But so that functions can do it automatically.
You won't need to sort the images, if I understand you correctly. You can order by the reference, or any other field, in your database.
0

Hope you are not storing your images on MySQL, please if you do, desist from that detrimental act because it is going to make your database unnecessarily heavy...

1 Comment

That is a blanket statement be carefull. I have had blobs stored in a database with a user base of over 10 million and 250 000+ concurrent connections and had no problems. It depends on your physical architecture i.e. disk layout etc.
0

I would recommend not storing your images in a database. While it is technically possible, it has serious performance concerns. Best practice would be to designate a folder for the images, then access them directly. If you'd like the filtering and sorting ablities of MySQL, then replace the BLOB column that currently stores the image data with a VARCHAR containing the file name.

<?php $conn = mysql_connect ("localhost","root","arctic123"); $db = mysql_select_db ("user_images", $conn); $imgdir = "/path/to/image/directory/"; if(!$db) { echo mysql_error(); } $q = "SELECT * FROM images"; $r = mysql_query ("$q",$conn); if($r) { while($row = mysql_fetch_array($r)) { echo $row['username']; echo "<br>"; echo "<img src='" . $imgdir . $row['logo'] . "' />"; echo "<br>"; } }else{ echo mysql_error(); } ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.