tags:

views:

59

answers:

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;

?>
+9  A: 

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.

Matt
+1 A file/data stream is either `image/jpg` or `text/html`, not both.
MvanGeest
How should i display the image inside the table then?
Satish
@Satish a separate request should be sending the table which has an `<img src="path/to/your/image/script"/>`
Matt
@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
@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>`
Matt
@matt: I have edited my question...please have a look
Satish
@Satish can you browse to image.php directly? Do you see an image?
Matt
@Matt:No still i don't get the image in the browser.
Satish
@Satish Post the full image.php source? What do you see if view the source on the browser?
Matt
@Matt: On viewing the source, the code that i see is <table><tr><td><img src="image.php"/> </td></tr></table>
Satish
@Satish - right, but when you browse to `image.php` *directly* .. you don't see an image, right? Can you view-source on *that*? Also, please post your current image.php code in its entirety ..
Matt
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;?>
Satish
@Satish and what do you see when you view source that? Is this available online somewhere?
Matt
@Matt: I dont know the specific name for such form of image but you can consider it as thumbnail displaying no image.
Satish