1

hello i'm a newbie in php and i'm working in my project right now. i use mysql as my database and i'm using object oriented style of coding. my problem is that i don't have any idea of how to display image using object oriented style.. here is my codes:

<?php
$sql2="select id, name, item_price, item_size, item_color, picture, category, type from item ";
$result2 = $conn->query($sql2);

if ($result2) {
    while ($row = $result2->fetch_row()) {
        ?>  
        <tr>
            <td><?= $row[0]?></td>
            <td><?= $row[1]?></td>
            <td><?= $row[2]?></td>
            <td><?= $row[3]?></td>
            <td><?= $row[4]?></td>
            <td><img src="data:image/png;base64,<?= $row[5]?>" alt="" /></td>
            <td><?= $row[6]?></td>
            <td><?= $row[7]?></td>
        </tr>
        <?php
    }
}
?>

all column are able to display the data except the image does not working or not displaying image. any idea is appreciated.. i'm sorry about my English..

Jazi
  • 6,569
  • 13
  • 60
  • 92
  • First thing, don't use short tags. They are deprecated. – Jazi Dec 16 '14 at 10:15
  • You should not store image binaries in your db, for tons of good reasons. Store your files in your web server, and the url of the files in your database. So you will only have to make ALTDESC – DooKie Dec 16 '14 at 10:17
  • there is no way to display image from database using $result->fetch_row in php-unless you properly provide the path to the src of img tag. – Ataboy Josef Dec 16 '14 at 10:18
  • 1
    This is seems to me ok syntactically, so maybe your data has damaged. But anyway, as others mentioned, do not store them in db. – vaso123 Dec 16 '14 at 10:18
  • 1
    @KrzysztofTrzos Short tags (`...?>`) aren't deprecated, but are discouraged since they rely on the [`short_open_tag`](http://php.net/manual/en/ini.core.php#ini.short-open-tag) setting being enabled. The shorthand `echo` tags (`=...?>`) are, since 5.4.0, available regardless of the `short_open_tag` setting. – timclutton Dec 16 '14 at 20:01
  • @timclutton Hah! Didn't know that. Do You have maybe some reference or article about this? – Jazi Dec 17 '14 at 20:11

1 Answers1

3

Do not store images in You database.

Example reference: Can I store images in MySQL

Save Your images in somewhere on Your public server and save paths to them in your database (then, You should use varchar types of columns in your table).

After that, You can use this data (as mentioned in comments):

<img src="YOUR_BASE_PATH/<?php echo $row[5] ?>" alt="<?php echo $altdesc ?>" />
Community
  • 1
  • 1
Jazi
  • 6,569
  • 13
  • 60
  • 92
  • Krzysztof Trzos, that's good to hear.. but what i like to achieve is to display the picture that save from maysql database.. – Heinrich Lee Tamdang Dec 16 '14 at 11:50
  • @HeinrichLeeTamdang You can store images in `BLOB` type of MySQL column. Here You have example how to display it: http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-your-images/ – Jazi Dec 16 '14 at 12:09