php中的标题问题

时间:2010-06-19 15:32:01

标签: php header

考虑以下代码

<?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

}

?>  

当我评论X和Y行时,图像会显示,否则不会。可能的原因是什么?

编辑:遵循马特的建议。

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;

即使这样做,我也没有得到预期的结果。

编辑:      代码'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 个答案:

答案 0 :(得分:9)

因为当浏览器被告知MIME类型为image/jpg时,它最不希望看到的是<table ...

当您设置MIME类型时,您告诉浏览器“我正在向您发送图像”。但是,HTML标记肯定不是图像数据,因此浏览器不知道如何呈现它。