0

Okay, so my end goal is to display an image (or multiple in a blog type style) that is stored in a database(MySQL) as a blob. Right now it display a broken image icon (enter image description here). This is what I've tried so far:

<?php
    $link = mysql_connect('HOST','USER','PASS!') or die("Could not connect to database");
    $dbsel= mysql_select_db('DATABASE', $link) or die("Couldn't select database.");

    $result = mysql_query("SELECT * FROM bmblog");

    while($row = mysql_fetch_assoc($result))
    {

        echo "<img src='php/imgView.php?imgId=".$row['media']."' />";

        echo "<center>" . "<font color='white'>" . "<FONT FACE='timesnewromans'>" . nl2br(htmlspecialchars($row['msg']));

        echo "<br>";
        echo "<br>";
    };

?>

Also

echo "<img src='data:image/jpeg;base64," . base64_encode($row['media']) . '" />';

echo "<center>" . "<font color='white'>" . "<FONT FACE='timesnewromans'>" . nl2br(htmlspecialchars($row['msg']));

Both display the same broken image icon. I did notice however without trying to echo the second line (of text) I did get a full boarder around the broken image icon that would have been about the size of the image. This is not the same for the first method though. I've been searching for a few hours now and have found tons of post about this, but none that seemed to work for me or make sense, I am fairly new to PHP so this may be something simple I am missing; Either way thank you in advance for any help it is greatly appreciated!

falsetru
  • 357,413
  • 63
  • 732
  • 636
Steve Byrne
  • 1,320
  • 1
  • 16
  • 29
  • Are you using Internet Explorer to test this perhaps? – putvande Aug 03 '13 at 11:45
  • what code is in imgView.php? – Egg Vans Aug 03 '13 at 11:45
  • I have tried: Opra, Chrome, Internet Explorer, and Firefox; same issue with them all :/ – Steve Byrne Aug 03 '13 at 11:46
  • None, it does not exist. I'm guessing this is my problem (I took that code from another answer that was not explained at all, just the code)? – Steve Byrne Aug 03 '13 at 11:48
  • look here [link](http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php) – Egg Vans Aug 03 '13 at 11:57
  • After trying that it still doesn't work (now it displays nothing at all) and gives this message: Warning: Cannot modify header information - headers already sent by (output started at C:\WampDeveloper\Websites\THEWEBSITE.com\webroot\index.php:202) in C:\WampDeveloper\Websites\THEWEBSITE.com\webroot\index.php on line 209 Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\WampDeveloper\Websites\THEWEBSITE.com\webroot\index.php on line 210 – Steve Byrne Aug 03 '13 at 12:04

1 Answers1

0

What might solve the problem is the to add header("Content-Type: image/jpeg"); just before you print/echo the image.

Also it is strange that base64 example you included would not have worked. When I looked at the example provided, the first line of the Also section the echo opens with double quote(") and end with single quote('). This might cause exception and why you couldn't test base64 properly.

It must be:

echo "<img src='data:image/jpeg;base64," . base64_encode($row['media']) . '" />";

You can also add a charset: data:image/jpeg;charset=utf-8;base64,

Lastly, I would suggest looking at the following example that might help

How to Base64 encode your images

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27