How to display images from a mysql db with PHP

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 12668 Views - Last Post: 25 July 2011 - 10:34 AM Rate Topic: -----

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

How to display images from a mysql db with PHP

Posted 23 July 2011 - 12:07 PM

I have some images in a mySQL db. I know they are there because I can see them in Query Builder. I am trying to display a single image using a PHP query, but i keep getting this error:

"The image “http://localhost/work/pregnancy/handle.php” cannot be displayed because it contains errors."

The image is stored as a BLOB. Here is my code to retrieve the image:


mysql_connect($dbhost, $dbuser, $dbpass) or die("Can not connect to database: ".mysql_error());

mysql_select_db($dbname) or die("Can not select the database: ".mysql_error());  

$query = mysql_query("SELECT * FROM images_test WHERE pid=3");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');

     echo $content;
?>



I know the script works in general, because I changed it a bit to retrieve the pic id, and that displayed fine, so it is just when pulling an image. Any hints, ideas or a good tutorial would be great. thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to display images from a mysql db with PHP

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 01:09 PM

Was it a JPG type originally?
Was This Post Helpful? 0
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 01:23 PM

Not totally sure what you mean, but the 3 images I uploaded are all .jpg images that I had laying around on my hard drive, and uploaded them for testing purposes. I tried to retrieve all3 (one at a time) and all received the same error message.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2932
  • View blog
  • Posts: 10,134
  • Joined: 08-August 08

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 01:30 PM

Why would you store the image in the database and not just its path?
Was This Post Helpful? 3
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 01:36 PM

I agree with CTphpnwb. Not only does storing the path make it easier to work with the image, but it reduces a lot of overhead when running queries.
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 02:05 PM

True he only needed to insert the file path into the database but teahou I'm thinking you didn't upload it correctly. What does your upload script look like?
Was This Post Helpful? 0
  • +
  • -

#7 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 02:44 PM

Hi codeprada. Here is the insert script:

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO images_test ";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}
else {
   print "No image selected/uploaded";
}
echo "<a href='index.html'>Go Home</a>";

// Close our MySQL Link
mysql_close($link);


?>



I used this script to upload 3 images, then I used mysql Query Builder to select them. In the results panel it has an image viewer, so I was able to see each image, which is why I am assuming they uploaded ok.

As to the 'why are you putting images in the DB' question, it is just for a test. I am using Amazon RDS for the first time, and I am not sure how it works, so I am just testing to see if I can put images up there and call them back. Usually I would put all the images in a folder, then have only the paths to the images as you guys said, but i don't know where to put the images yet.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 02:46 PM

EDIT: Note to follow-up readers: my advice/thoughts here on the requirement of addslashes in a binary situation is wrong, as elucidated below by those far smarter than I, so keep reading!

Agree with CTphpnwb and macosxnerd101 above re saving binary data in the DB, but this

$data = addslashes($data);


seems like a bad idea. Not having stored BLOBs in the DB before, I don't know if it's necessary, but it seems errorific on its face. If you're storing into a BLOB (a Binary Large OBject), there should be no purpose to adding slashes to the data, and would seem to be a great way to corrupt it on save.

Is the image datatype a BLOB?

This post has been edited by JackOfAllTrades: 24 July 2011 - 04:51 AM
Reason for edit:: Fixed attribution/mea culpa

Was This Post Helpful? 0
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 02:50 PM

View PostJackOfAllTrades, on 23 July 2011 - 10:46 PM, said:

Agree with CTphpnwb and codeprada above, but this

$data = addslashes($data);


seems like a bad idea. Not having stored BLOBs in the DB before, I don't know if it's necessary, but it seems errorific on its face. If you're storing into a BLOB (a Binary Large OBject), there should be no purpose to adding slashes to the data, and would seem to be a great way to corrupt it on save.

Actually, addslashes is pretty much required here incase the image contains a ' or something (which is highly unlikely, but still).

It won't corrupt the data entry since the slashes are simply to escape the string when being entered.

But as with what everyone else has been saying, storing image data in the DB is a bad idea. I hate the fact that vBulletin stores it's attachments there by default. It makes backups and restoration an absolute PitA!!

This post has been edited by RudiVisser: 23 July 2011 - 02:51 PM

Was This Post Helpful? 1
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 03:23 PM

So you need to addslashes to a BLOB? Interesting. I mean, I'm ASSUMING the OP is saving into the most appropriate datatype for the data, which in this case would be a BLOB, right?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 03:29 PM

In my opinion, you might be better off investing some time to find out where you need to store your resources so you won't have to go back and make significant changes to your program later. Why rewrite the same functionality adhering to standards, when you can do it the first time? It is easier to do it right the first time than to refactor later.
Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 03:37 PM

Another reason to stop using MySQL
http://www.java-samp...?tutorialid=930

Your life would be a lot easier if you dealt with the image path instead of the it binary.

The slashes issue can be avoided with prepared statements. I really think PHP should declare MySQL deprecated.
Was This Post Helpful? 0
  • +
  • -

#13 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 04:09 PM

View PostJackOfAllTrades, on 23 July 2011 - 11:23 PM, said:

So you need to addslashes to a BLOB? Interesting. I mean, I'm ASSUMING the OP is saving into the most appropriate datatype for the data, which in this case would be a BLOB, right?

It's not about that though, I mean if you're inserting like INSERT INTO table VALUES ('$data') you can't insert something with a ' in it, it's going to die. The slashes are obviously not going to be in the column.
Was This Post Helpful? 2
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: How to display images from a mysql db with PHP

Posted 23 July 2011 - 06:24 PM

View PostJackOfAllTrades, on 23 July 2011 - 10:46 PM, said:

Agree with CTphpnwb and codeprada above, but this

$data = addslashes($data);


seems like a bad idea.

If you are putting the image into a old-school mysql_query string you'll have to escape it so it won't mess up the query. Even though it's binary data PHP reads it into the query string as a string of bytes (byte = char = a whole lot of random text), which may well include non-safe characters that'll cause errors when it's parsed.

You're still going to want to use the mysql_real_escape_string function instead of addslashes. There are a couple of chars that the binary data may coincidentally include that the latter does not cover.

To quote the manual entry for mysql_real_escape_string.

php.net/mysql_real_escape_string said:

If binary data is to be inserted, this function must be used.

This post has been edited by Atli: 23 July 2011 - 06:25 PM

Was This Post Helpful? 2
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: How to display images from a mysql db with PHP

Posted 24 July 2011 - 04:48 AM

Yeah, I apologize for the bad information. I've never had a need to save binary data to a DB, so I made the horrible assumption that the DB, on inserting into a binary field, would sort of "do the right thing". Should never assume, obviously.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2