0

How do i get a file as an image from a database using php?I've tried this however all I get is the name of the image not the actual image. Is there any specific syntax that must be used? Also the Image type in the database is of type BLOB so it should store the image .

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "WebAssignment";
$password ="qwer12";
$dbname = "webassigment";

// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT Food_Name, Food_Price,Food_Category, Image FROM menu";
$result = $conn->query($sql);

$array = array();

while($row =$result->fetch_assoc()){
  // add each row returned into an array
  $array[] = $row;


}
  ?>
    <h2>Starters</h2>



    <?php
for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="starters"){
            echo $array[$i]["Food_Name"]. " " . $array[$i]["Food_Price"]. "<br>" . $array[$i]["lastname"]. "<br>".$array[$i]["Image"]."<br>";
        }

    }
    ?>
    <h2>Burgers</h2>

    <?php
for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="burgers"){
            echo $array[$i]["Food_Name"]. " " . $array[$i]["Food_Price"]. "<br>" . $array[$i]["lastname"]. "<br>".$array[$i]["Image"]."<br>";
        }

    }
    ?>
    <h2>Desserts</h2>
<?php
for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="desserts"){
echo $array[$i]["Food_Name"]. " " . $array[$i]["Food_Price"]. "<br>" . $array[$i]["lastname"]. "<br>".$array[$i]["Image"]."<br>";       }

    }
    ?>
    <h2>Drinks</h2>

    <?php
for($i=0;$i<sizeof($array);$i++) {
    if($array[$i]["Food_Category"]=="drinks"){
echo $array[$i]["Food_Name"]. " " . $array[$i]["Food_Price"]. "<br>" . $array[$i]["lastname"]. "<br>".$array[$i]["Image"]."<br>";       }

    }



$conn->close();
?>
</body>
</html>
Cz147
  • 11
  • 2
    You first need to use the `` tag with a `src`. This is HTML 101 stuff. – Funk Forty Niner Apr 05 '16 at 13:23
  • If you got the name you are storing the location in the db, not the image. See ^ for how to display. If you order by `Food_Category` you can probably do this all in one `foreach` loop, you also can use `foreach` in place of your `for` usage. – chris85 Apr 05 '16 at 13:23
  • @chris85 I'd say you've pretty much give the OP the answer to their question there. Why not post it as an answer with a little more detail. – CyberneticianDave Apr 05 '16 at 13:27
  • @CyberneticianDave sounds like OP wants images stored in DB so might not be correct. Also with `order` approach the burgers will be first or last and `starters` will be the opposite, not sure if that is important or not. So it could be an answer but it also could be wrong approach.. – chris85 Apr 05 '16 at 13:34

1 Answers1

0

This question was already raised and was answered by daiscog and improved by Eric Leschinski at How to retrieve images from MySQL database and display in an html tag

According to "em, you need to create a php file (includes DB info with image header declarations) which you can call using simple HTML image tag as simple as this-

<img src="<name of your php file>">
Community
  • 1
  • 1
Disk01
  • 347
  • 3
  • 11