5

I have a database containing movies Name, their description and their cover picture. The cover picture field type is as blob and the problem is that I can't retrieve it from the database. I want to display the movie name along their cover picture beside them... How to do it.. Here is my code..

<?php

include ("php/connection.php");
$ID = $_GET['id'];
$listmovies = "SELECT * FROM movies where Genres LIKE '%$ID%'";
$result = mysql_query($listmovies);
while ( $row = mysql_fetch_assoc($result) ) {
    ?>
<table border="1" width="100%">
    <tr>
        <td rowspan="2" width="90" height="120">
<?php

    // set the header for the image
    header("Content-type: image/jpeg");
    echo $row['Image'];

    ?> </td>
        <td width="200" height="10">
<?php

    echo $row['Title'];
    ?></td>
    </tr>
    <tr>
        <td width="200" height="110"><a
            href="php/moredetails.php?id=<?php echo $row['ID']; ?>">More Detail</a></td>
    </tr>
<?php } ?> </table>

I just want to display The Imgaes beside the title of the movie?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Shubhum
  • 87
  • 8
  • 2
    Possibly useful: http://stackoverflow.com/questions/6106470/php-convert-a-blob-into-an-image-file – lc. Oct 17 '12 at 15:27
  • Also: http://stackoverflow.com/questions/5932249/show-a-blob-image-php-mysql-along-with-other-data which actually links back to http://stackoverflow.com/questions/5525830/displaying-an-image-stored-in-a-mysql-blob where I would check out [this answer which uses a data-URI](http://stackoverflow.com/a/5525862/44853) – lc. Oct 17 '12 at 15:28
  • 2
    Preferably you wouldnt be storing the images directly in the DB but rather a path to the image on the filesystem, which can then be translated to the URL. – prodigitalson Oct 17 '12 at 15:33
  • Add More detail and code on your implementation .... can you also remove the header to see if you have some syntax errors – Baba Oct 17 '12 at 15:39

2 Answers2

2

Yes it won't display because any output above header would always generate error ... you need to have a different page to output your image or include it has base64 image

Remove

header("Content-type: image/jpeg");
echo $row['Image'];

And add this :

printf("<img src=\"data:image/jpeg;base64,%s\" />",base64_encode($row['Image']));  
                               ^--- Note this is only for jpeg images
Baba
  • 94,024
  • 28
  • 166
  • 217
0

I suggest something like that:

Make a new php file called for example image.php; That file will replace physical image file. In that file you put the code from your post plus some logic to get image data from database;

In parent template(php file maybe) you put code for image:

<img src="image.php?id_movie=<?php echo $id_movie; ?>" width ="200" height="200" /> More Detail

In image.php i suggest to be careful at spaces outside php tags (at the beniging and at the end). Also to image.php you need to give id of the movie to know what image to load from database.

Moldovan Daniel
  • 1,521
  • 14
  • 23