Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have database
PHP
`image_name`, `caption`, `image`, `username`
I want to display the image according his name in database(image_name)I have this code the error in it the image is not show

PHP
<?php

 require_once("db.php");
 session_start();
 $username = $_SESSION['username'];
 $sql = "SELECT * from photo2 WHERE Username='$username'";
 $result = mysql_query($sql) or die (mysql_error());
 $numrows = mysql_num_rows ($result);
 if($numrows!=0)
   {
  while ($found_ids = mysql_fetch_assoc($result))
  {

  echo "<tr><td align='center'> $found_ids[image_name]</a></td>  </tr>";
      //echo "<img src=get.php?id=$found_ids[ID]";
   }
}

$id = addslashes($_REQUEST['image_name']);
$image = mysql_query("SELECT * FROM photo2 WHERE image_name=$id");
$image = mysql_fetch_assoc($image);
$image = $image['Photodata'];

header ("Content-type: image/jpeg");
echo $image;

?>
Posted

1 solution

you have very little knowledge of HTTP protocol.
Remove the last ?>

->You cannot print header after sending out html data.

->Your session should start ahead of including db.php

->you cannot send HTML data while you are sending image data. HTTP protocol did not stablish anything to achieve this.

->You have two distinct method POST or GET, using REQUEST is a bad idea.

to send image two methods can be used.

i) Convert the image into base64 encoded text and attach it with your html code. example(taken from wikipedia):
HTML
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

look "base64_encode" up

ii) second method is simpler: save the string in a file as image. then create the image link with the path. Example:
HTML
<img src="<?=$path?>" >
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900