kise tetsuya
kise tetsuya

Reputation: 21

how to display picture from data base? if i try to display it. it only shows the name of the image

enter image description here

here's my code i tried to use the base64 but the same problem occurs the img is already saved in my server/folder

$sql = "SELECT * FROM prods";
$result  = $con->query($sql);
if ($result->num_rows > 0) {


  echo "<table border='2'><tr><th>Name</th><th>Price</th><th>Photo</th><th>Stocks</th></tr>";
 
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["prodName"]."</td><td>" . $row["prodPrice"]. "</td><td>" . $row["prodPic"]
        /*'<img src="data:image;base64,'.base64_encode($row["prodPic"]).'" alt="Image" style="width: 10px; height: 10px;" >';*/."</td><td>" . $row["prodQuant"]. "</td></tr>";
    }
    echo "</table>";  

    }


?>
    </div>
    <div>
        <form action="up.php" method="POST" enctype="multipart/form-data">
            <input type="text" name="filename" placeholder="File name">
            <input type="text" name="fileTitle" placeholder="Image Name">
            <input type="text" name="fileDesc" placeholder="Price">
            <input type="file" name="file">
            <button type="submit" name="submit"> Upload </button>
    </div>

Upvotes: 0

Views: 41

Answers (2)

kise tetsuya
kise tetsuya

Reputation: 21

"<img src='img/" . $row["prodPic"]."'>"

Upvotes: 1

So I assume $row['prodPic'] is a string containing a relative path to an image file.

If that's the case, first you need the base directory in which you are saving all those images (assuming that you are saving them somewhere). Let's say that directory is /var/www/app/uploads and that exists in a variable called $uploadsDir.

Then, in the line where you have your base64_encode you should call base64_encode(file_get_contents($uploadsDir.DIRECTORY_SEPARATOR.$row["prodPic"])

Now, that is the easiest way to accomplish what you want, but it's a terrible one. Base64 inlined images make your document really heavy. It's better if you provide links to another script that will give your application the image. The browser will handle the image retrieval for you.

Upvotes: 0

Related Questions