0

I'm trying to display an image along with other information from the database.

PHP

  <?php
               mysql_connect("localhost","111","222") or die("Could not connect to localhost");
               mysql_select_db("movies") or die( "Could not connect to database");

               $result = mysql_query("SELECT * FROM allmovies");
               if ($result == false )  die(mysql_error()); 
               while($row = mysql_fetch_array($result))
               {
                    echo "<img src=' . $row['image'] . '>";
               ?>

Like:

Title: Blah
Price: Blah
Image: <img src=rarara">

All from MySQL in one page?

7
  • 1
    I hope you've got a good reason for using the ancient mysql_query interface. Commented Oct 4, 2013 at 17:40
  • what you are storing in db? its path or what? Commented Oct 4, 2013 at 17:41
  • I'm storing the .png file under as a blob Commented Oct 4, 2013 at 17:43
  • How are you storing the byte string? base64 encoded? Commented Oct 4, 2013 at 17:48
  • 2
    Well, if it's a png, then you shouldn't send jpeg headers. Commented Oct 4, 2013 at 17:56

2 Answers 2

2
  1. Don't store image data in a database, they are generally not suited to this and incurs extra overhead on your MySQL connections returning the data. You should be storing the path to the image file, and serving that.
  2. If you insist on doing it you you should only be returning one image at a time with the proper header based on the image type using something like the following:

    $imagedata = data_from_mysql();
    header('Content-Length: ' . sizeof($imagedata) );
    header('Content-Type: image/png');
    echo $imagedata;
    exit;
    
  3. If you really want to make your page source bloated, slow, unmanageable, and nigh-uncacheable:

    while( $imagedata = data_from_mysql() ) {
      echo "<img src='data:image/png;base64," . base64_encode($imagedata) . "'>";
    }
    

I cannot stress enough how these are terrible ideas that you should not use, but if you cannot listen to reason you can at least do bad things the right way.

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

5 Comments

@ Sammitch okay, thanks. I changed it to the path now. I'm trying to use echo "<img src=' . $row['image'] . '>";
You're mixing single and double quotes there, echo '<img src="' . $row['image'] . '" />'; or better yet printf('<img src="%s" />', $row['image']);
Are you saying you just changed the structure of your site and are now storing paths instead of image blobs? If so, consider relative or absolute paths. <img src="/uploads/images/<?php echo $row['image']; ?>"> where $row['image'] = something like 'entry-1.jpg' as opposed to storing a full path in your database. It makes more sense to store filename, then make the path a system variable in case you move things around
@KaiQing I'd recommend against absolute paths since that ties your code to the particular machine it's on at the time and makes changing hosts/machines/providers/OSes/etc a major pain.
@Sammitch - I mean writing absolute in the src attr of the tag instead of echoing $row['image'] as is. Assuming the db result is just a file name or something
1

You could use imagecreatefromstring()

$im = imagecreatefromstring($row['image']);
if ($im !== false) {
    ob_start();
    imagejpeg($im);
    $data = ob_get_contents();
    ob_end_clean();
    echo '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '" />';
}

Just my opinion, but it might be slightly more sane to save the images to the file server and then store a reference to the path instead of the whole image as a blob?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.