Grace Michelle Grace Michelle - 6 months ago 10
SQL Question

PHP - Show Image From Database

First of all, I've already known that there are similiar questions in stackoverflow about show images in php. But, my code is simple, & not complicated like the others, so I hope you can answer it in a simple way.

In database, I have

mahasiswa
table. It has these fields:

nrp | nama | pass | jatah_sks | foto_profil


I want the program to show
foto_profil
which is an image. The images are saved in folder
propic
. This is my code:

$sql4 = "select foto_profil from mahasiswa"
. " where nrp=".$nrp;
$result4 = mysqli_query($link, $sql4);
if (!$result4) {
die("<h3>SQL Error</h3>" . $sql4);
}
$row4 = mysqli_fetch_array($result4);

<img src="propic/<?php echo $row4['foto_profil'] ?>"/>


The code shows no errors, but
foto_profil
won't showed up. It just shows an break-image icon. How is the right code? Please explain your answer. Thanks

Answer

You are mixing php and html here.. You want this:

$sql4 = "select foto_profil from mahasiswa"
        . " where nrp=".$nrp;
$result4 = mysqli_query($link, $sql4);
if (!$result4) {
      die("<h3>SQL Error</h3>" . $sql4);
}
while ($row4 = mysqli_fetch_array($result4)) {
    echo '<img src="propic/' . $row4['foto_profil'] . '"/>';
}