3

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.

6
  • point of note. In your while loop, that <tr> on the third line of your code within the loop should be </tr> Commented Jul 8, 2017 at 5:23
  • 1
    what you have in database.php? untill we see that,we can't tell what problem you are getting Commented Jul 8, 2017 at 5:24
  • show your database.php too Commented Jul 8, 2017 at 5:24
  • A side note: your database query is wide open to sql injection attacks. Please learn about the security benefits of using the combination of "prepared statements" and "parameter binding". Commented Jul 8, 2017 at 5:26
  • Did you check connection error for mysqli? you can't blindly replace all mysql_ functions with mysqli_ Commented Jul 8, 2017 at 5:35

1 Answer 1

1

You can follow below steps to DEBUG and find out where is the actual problem.

Step 1 : Update your Image src

src='prac_img_display.php? id=".$row['sub_id']."'
/*TO*/
src='prac_img_display.php?id=".$row['sub_id']."'

Note : Because, there is a Blank space.

Step 2 : Browse your Image URL in Browser, like

example.com/prac_img_display.php?id=8

Note : Check, If the image Display properly In your browser window.

IF, Not displaying Image properly

Step 3 : Check header("Content-type: image/jpeg"); is correct.

Suppose, If you uploaded PNG image, This will not work.

For PNG image you should use header("Content-type: image/png");

Note : Also don't forget to DEBUG SQL Result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.