简体   繁体   中英

header("Content-Type: image/png"); php

when implement this code i have no image

  <?php

  include('confing.php'); 
  echo '<img src="getImage.php?id=2" >';

  ?>

file => getImage

<html >
<body>
<?php

include('confing.php'); 

$id = $_GET['id'];
$sql="SELECT * FROM images WHERE id=$id ";

$result=mysqli_query($connection,$sql);

$row = mysqli_fetch_assoc($result);

    // Set the content type header - in this case image/png
    header("Content-Type: image/png");
    echo $row['content'] ;

?>

body> html> body html body> html> body html body> html> body html 在此处输入图片说明

The errors you are getting:

  • Undefined index ID : It would appear that you are calling it without passing the ID argument. Obviously you should check that. But also, you should add some error checking to your code to ensure that it doesn't crash if the ID is not passed, or if the ID is invalid.

  • Undefined variable $connection : Your database connection variable is not set. Maybe you config.inc include doesn't actually set the connection? Or maybe you've got the variable name wrong? You need to check your config.inc to find out why this is happening. In any case, this leads directly onto the next error...

  • mysqli_query expects connection parameter : Because the $connection variable is not set, your DB query can't be run. Some error checking here would be helpful, even if you do sort out the connection variable, as there may be other reasons the connection may fail.

  • mysqli_fetch_assoc expects result parameter : This occurs because the result variable is not set due to the query not being run. You should add some error checking for this as well, since there may be other reasons why a query fails to run.

The errors didn't even get as far as hitting the header() function call, which would also fail because the program has already output some content. You should remove the <html><body> tags from the top of the program, as they are not necessary for outputting an image; in fact, they would cause the image to be invalid.

Your line:

echo '<img src="getImage.php?id=2">';

will output this into your html page:

<img src="getImage.php?id=2">

Second, it will not load, execute and insert the result of getImage.php.

Here's a solution:

First page:

<?php
include('getImage.php');
$id = $_GET['id'];
$img = getImage($id);
echo "<img src='$img' >";
?>

getImage.php:

<?php
include('confing.php');

function getImage($id){
    $sql="SELECT * FROM images WHERE id=$id ";
    $result=mysqli_query($connection,$sql);
    $row = mysqli_fetch_assoc($result);
    return $row['content'] ;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM