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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
exxcellent
  • 659
  • 4
  • 11
  • 18
  • 1
    _Whaaaat_ issues? You need to say what your code is doing wrong at the moment. – Bojangles Oct 17 '11 at 11:22
  • 1
    You can start by fetching those results – Damien Pirsy Oct 17 '11 at 11:23
  • i am not clear of the syntax involved to display the BLOB image – exxcellent Oct 17 '11 at 11:25
  • @DamienPirsy How can i fetch these results? – exxcellent Oct 17 '11 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 '11 at 11:29
  • @john thanks! however is there tutorials on that? i actually have a folder in my localhost that contains all my images. how can i reference these paths in my database? – exxcellent Oct 17 '11 at 11:38

5 Answers5

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;
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
daiscog
  • 11,441
  • 6
  • 50
  • 62
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.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • 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 '11 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" />";
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
James Williams
  • 4,221
  • 1
  • 19
  • 35
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>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Erick Mwazonga
  • 1,020
  • 12
  • 25
-2

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

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

Punit
  • 1,110
  • 1
  • 8
  • 14
  • 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 '11 at 13:34