John
John

Reputation: 4944

Image from MySQL database not printing

The query and HTML table are printing out $row["title"], the date, and row["text1"] just great. However, there is a problem with $row["file_data"], which is supposed to be an image stored in a MySQL database. It's printing out as a large number of characters from languages other than English, i. e. BMö26(šÀ2ÿÿÿÿÿÿÿÿ, etc. for several dozen rows.

How can I make the image appear?

Thanks in advance,

John

$sqlStr = "SELECT s.title, s.datesubmitted, s.text1, s.text2, s.text3, s.cleanurl1, s.cleanurl2, s.cleanurl3, s.submissionid, i.image_id, i.filename, i.mime_type, i.file_size, i.file_data, i.photonumber
      FROM submission s
      JOIN images2 i on s.submissionid = i.submissionid   
  GROUP BY s.submissionid
  ORDER BY s.datesubmitted DESC 
     LIMIT $offset, $rowsperpage";          

$tzFrom = new DateTimeZone('America/New_York'); 
$tzTo = new DateTimeZone('America/Phoenix'); 



// echo $dt->format(DATE_RFC822); 





$result = mysql_query($sqlStr);

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

$arr = array(); 
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) { 

    //header('Content-type: ' . $row['mime_type']);
//header('Content-length: ' . $row['file_size']);

    $dt = new DateTime($row["datesubmitted"], $tzFrom); 
    $dt->setTimezone($tzTo);


    echo '<tr class="class3a">';
    echo '<td class="sitename1"><a href="http://www...com/blog">'.$row["title"].'</a></td>';
    echo '</tr>';
    echo '<tr class="class3b">';
    echo '<td class="sitename2name">'.$dt->format('F j, Y &\nb\sp &\nb\sp g:i a').'</td>';

    echo '</tr>';
    echo '<tr class="class3c">';
    echo '<td class="sitename2">'.$row["text1"].'</td>';
    echo '</tr>';

    echo '</tr>';
    echo '<tr class="class3c">';
    echo '<td class="sitename2">'.$row["file_data"].'</td>';
    echo '</tr>';



    }
echo "</table>";

Upvotes: 1

Views: 1633

Answers (2)

RobertPitt
RobertPitt

Reputation: 57268

The reason for this is because the image data stored within the database is of the an image content type, meaning it's an image and not text.

you cannot print image data directly into a text page, you have to push it out within it's own entity, adding the correct content types within the headers so the browser know's how to process it.

what you should be doing is creating a separate file called image.php and get that to produce the image on behalf of you.

for example:

<?php

if(!empty($_GET['id']) && is_numeric($_GET['id']))
{
     //fetch the information for the database
     //if the blob data is of the content type image/gif for instance
     header('content-type: image/gif');
     echo $result['image_data'];
}

and then within your html page, ask the image.php to produce the image for oyu.

 <img src="image.php=<?php echo $result['image_id'] ?>" />

Upvotes: 1

Sam Bisbee
Sam Bisbee

Reputation: 4441

It's because your browser doesn't realize that the data you're sending back is an image. When your web server responds to a request it specified the type of content it is (hence the Content-Type header), and your page is being specified as text. This is why image tags are used: they give you a chance to say "embed this other resource in this location". What your code does is dump the binary data of the image as text onto the screen - not what you want.

What you need to do is create another PHP page, such as getImage.php, that accepts a $_GET parameter (ie., a row id). That page will then query the database and echo the image data, specifying the Content-Type header.

Here is some proof of concept code that I wrote without testing and that does not handle SQL injection, or a number of other potential issues.

header('Content-Type: image/png'); //change to the proper content type for your type of image

$imageID = mysql_real_escape_string($_GET['q']);

$result = mysql_query(sprintf('SELECT file_data FROM images2 WHERE id="%s" AND file_data IS NOT NULL LIMIT 1', $_GET['q']));

if(mysql_num_rows($result) !== 1)
{
  //a row wasn't found, so 404
  header('HTTP/1.0 404 File Not Found');
}
else
{
  $row = mysql_fetch_object($result);
  echo $row['file_data'];
}

Now, when you're building your HTML in your existing file, you would do something like this:

echo '<td class="sitename2"><img src="./getImage.php?q='.$row["id"].'"/></td>';

Adjusting your SQL column names accordingly.

Cheers.

Upvotes: 1

Related Questions