考虑以下代码
<?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;
?>
答案 0 :(得分:9)
因为当浏览器被告知MIME类型为image/jpg
时,它最不希望看到的是<table ...
当您设置MIME类型时,您告诉浏览器“我正在向您发送图像”。但是,HTML标记肯定不是图像数据,因此浏览器不知道如何呈现它。