34

I created a MySQL database with a table using phpmyadmin. I created this table with a BLOB column to hold a jpeg file.

I have issues with regards to the php variable $result here.

My code so far: (catalog.php):

<body>
<?php
  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT dvdimage FROM dvd WHERE id=1";
  $result = mysql_query("$sql");
  mysql_close($link);

?>
<img src="" width="175" height="200" />
</body>

How can I get the variable $result from PHP into the HTML so I can display it in the <img> tag?

6
  • 1
    Whaaaat issues? You need to say what your code is doing wrong at the moment.
    – Bojangles
    Oct 17, 2011 at 11:22
  • 1
    You can start by fetching those results Oct 17, 2011 at 11:23
  • i am not clear of the syntax involved to display the BLOB image
    – exxcellent
    Oct 17, 2011 at 11:25
  • @DamienPirsy How can i fetch these results?
    – exxcellent
    Oct 17, 2011 at 11:26
  • 5
    Just my 2 cents and not related to your question, but I think you'd be better off storing the images on your file system and just referencing the path in your database. You'll get better performance, and more flexibility as to where you store your images.
    – John
    Oct 17, 2011 at 11:29

5 Answers 5

43

You can't. You need to create another php script to return the image data, e.g. getImage.php. Change catalog.php to:

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

Then getImage.php is

<?php

$link = mysqli_connect("localhost", "root", "", "dvddb");
$sql = "SELECT dvdimage FROM dvd WHERE id=?";
$result = mysqli_execute_query($link, $sql, [$_GET['id']]);
$image = mysqli_fetch_column($result);

header("Content-type: image/jpeg");
echo $image;
7
  • but the thing is,I have nearly 50 images in my db.You have written for getting only one image.How can i display whole images in the page May 3, 2013 at 8:53
  • 2
    To get a different image from the DB you simply change the id get parameter: <img src="getImage.php?id=1" width="175" height="200" /> <img src="getImage.php?id=2" width="175" height="200" /> ...
    – daiscog
    May 3, 2013 at 14:18
  • 1
    This answer is incorrect in that it IS possible, view Ilmari's answer. Jan 7, 2016 at 3:18
  • 1
    @DheerajMPai That is the entire script. Although these days you'd use MySQLi instead of the mysql_* functions (this answer is 7 years old).
    – daiscog
    Oct 12, 2018 at 12:37
  • 1
    It may be 7 year old but still helpful for beginners. Anyway I shifted for python. So no issues anymore!@megaflop
    – Trect
    Oct 16, 2018 at 13:42
33

Technically, you can too put image data in an img tag, using data URIs.

<img src="data:image/jpeg;base64,<?php echo base64_encode( $image_data ); ?>" />

There are some special circumstances where this could even be useful, although in most cases you're better off serving the image through a separate script like daiscog suggests.

1
  • 1
    I was wondering whether someone would suggest a data URI. Not very efficient, IMO (no browser/proxy caching), but certainly worthy of a note.
    – daiscog
    Oct 17, 2011 at 16:04
4

In case you store only the filename, while the image itself is stored in a file

while($row = mysqli_fetch_array($result)) {
    echo 'img src="',htmlspecialchars($row['filename']),'" width="175" height="200" />";
}
0

Using PDO

Query

<?php
require 'connect.php';

$stmt = $pdo->query("SELECT * FROM products");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

View

<table>
    <thead>
        <tr>
            ...
            <th>Product Name</th>
            <th>Product Image</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($products as $product) : ?>
            <tr>
                ...
                <td><?= htmlspecialchars($product['product_name']); ?></td>
                <td>
                    <img 
                        src="data:image/jpeg;base64,<?= base64_encode($product['image']); ?>" 
                        width="100" 
                        height="100"
                    >
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
-2

add $row = mysql_fetch_object($result); after your mysql_query();

your html <img src="<?php echo $row->dvdimage; ?>" width="175" height="200" />

1
  • You can't put raw image data inside an img src. OP clearly states the database stores the image data, not a path to the image file.
    – daiscog
    Oct 17, 2011 at 13:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.