1

Consider the following code

<?php

 $username = "root";
 $password = "";
 $host = "localhost";
 $database = "binaries";

 @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

 @mysql_select_db($database) or die("Can not select the database: ".mysql_error());

 $id = 5;

 if(!isset($id) || empty($id)){
 die("Please select your image!");
 }else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['imag'];
header('Content-type: image/jpg');
echo '<table><tr><td height="700" width="700">';// Line X
print $content;

echo '</td></tr></table>';//Line Y

}

?>  

When I comment the lines X and Y the image gets displayed, otherwise not.What could be the possible reason?

EDIT: After following Matt's advice.

show.php

 echo '<table><tr><td>
  <img src="image.php"/>
  </td></tr></table>';

image.php

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['imag'];
header('Content-type: image/jpg');

print $content;

Even after doing this I am not getting the expected result.

EDIT: code of 'image.php' :

 <?php
  $username = "root";
  $password = "";
  $host = "localhost";
  $database = "binaries";

  @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

 @mysql_select_db($database) or die("Can not select the database: ".mysql_error());




 $query = mysql_query("SELECT * FROM tbl_images WHERE id=5");
 $row = mysql_fetch_array($query);
 $content = $row['imag'];
 header('Content-type: image/jpg');
 echo $content;

?>

1 Answer 1

9

Because when the browser is told the MIME type is image/jpg, the last thing it is expecting to see is <table ...

When you set the MIME type, you are telling the browser "I am sending you an image." However, HTML markup is certainly not image data, so the browser doesn't know how to render it.

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

13 Comments

+1 A file/data stream is either image/jpg or text/html, not both.
How should i display the image inside the table then?
@Satish a separate request should be sending the table which has an <img src="path/to/your/image/script"/>
@Matt:Please make me more clear where it should be written in this script.How can a path of image be determined when it is fetched from database?
@Satish No matter what's in the database, the name of your image script is the same. Let's say your above code is in a file called image.php. To display it in a table, have another file send <table><tr><td><img src="path/to/image.php"/></td></tr></table>
|

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.