2010-06-19 53 views
1

頭看看下面的代碼問題在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()); 

$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的圖像被顯示出來,否則not.What可能是可能的原因是什麼?

編輯:以下馬特的意見後。

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; 

?> 

回答

9

因爲當瀏覽器被告知的MIME類型爲image/jpg,它期待看到的最後一件事是<table ...

當你設置MIME類型,你告訴瀏覽器「我給你發送一個圖像。」但是,HTML標記肯定不是圖像數據,所以瀏覽器不知道如何呈現它。

+0

1的文件/數據流或者是'圖像/ jpg'或'文本/ html',而不是兩個。 – MvanGeest 2010-06-19 15:35:28

+0

我應該如何在桌子內部顯示圖像? – Satish 2010-06-19 15:37:22

+1

@Satish一個單獨的請求應被髮送它有一個'' Matt 2010-06-19 15:38:41