Ajmal Hossain
Ajmal Hossain

Reputation: 55

My PHP code for retrieving image is ok but no image is shown

This is my getImage.php page for php code

<?php

    if(isset($_GET['email'])){

        $con = mysqli_connect("localhost", "root", "") or die(mysqli_error($con));
        mysqli_select_db($con,"ajmal") or die(mysqli_error($con));

        $sql = mysqli_query($con,"SELECT image FROM users WHERE email='$email'");

        while ($row = mysqli_fetch_array($sql)) {
            $imageData = $row["image"];
        }

        header("Content-type: image/jpeg");
        echo $imageData;
    }
    else{
        echo "Error";
    }
?>

and this is my img tag where i use getImage.php page

<img src="getImage.php?email = $email" width="100" height="100">

and this is the output

enter image description here

what is the problem with it and what is its solution?

Upvotes: 0

Views: 55

Answers (1)

Mike Doe
Mike Doe

Reputation: 17566

Have you checked how your HTML is generated at all?

<img src="getImage.php?email=<?php echo $email ?>" width="100" height="100">

1) in your script, if the e-mail is missing you should set the error code if the response is invalid:

else {
    http_response_code(400);
    echo 'E-mail missing';
    exit;
}

2) your script is susceptible to SQL injection attack! Use MySQLi prepared statements as soon as possible.

Upvotes: 2

Related Questions