how to display images in php from database using MYSQLi queries?

Viewed 1260

Below is the code from where my page jumps to the "prac_image_display" to display images:-

<?php
    session_start();
    include("database.php");
    $sql = mysqli_query($con,"select * from mst_subject");
    echo "<h2><table cellpadding=5>";
    while($row = mysqli_fetch_array($sql)){
        echo "<tr>";
        echo "<td><img height='50' width='50' src='prac_img_display.php? id=".$row['sub_id']."'></td><td><a href=showtest.php?subid={$row['sub_id']}>".$row['sub_name']."</td>";
        echo "<tr>";
        //$_SESSION['subid']=$row['sub_id'];
    }
    echo "</table></h2>";
?>

And now below is the code written in prac_image_display.php file:-

        <?php

       if(isset($_GET['id']))
       {
        include("database.php");
        $sql = "SELECT image FROM mst_subject WHERE 
       sub_id=".$_GET['id'].";";
       //$sql = "SELECT image FROM mst_subject WHERE sub_id=8";
        $result=mysqli_query($con,"$sql");
        header("Content-type: image/jpeg");
        echo mysqli_result($result,0);
        mysqli_close($con);
     }
     else 
     {
           echo 'Please use a real id number';
     }
    ?>

Below is the code written in "database.php" file :-

    <?php
    $con=mysqli_connect("localhost","root","","test") or die("could not 
                         connect ");
    //mysql_select_db("test",$con)  or die("Could connect to Database");
    ?>

The images are stored in the database in BLOB. The problem is when I use MYSQL in both .php files as shown above it runs fine and all the images get displayed on localhost perfectly but when I replace MYSQL functions with MYSQLi functions the images do not get displayed. please, tell me what changes can I do here.

1 Answers
Related