无法在PHP中显示图像

时间:2011-09-29 01:40:37

标签: php mysql image lamp

这是我的图像显示代码 -

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

@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 mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1");
$row = mysql_fetch_array($query);
$content = $row['Image'];
header('Content-type: image/jpg');
echo $content;

这是我得到的错误

无法显示图像“http:// www ....”,因为它包含错误。

有什么不对? mysql中字段的数据类型是mediumblob

3 个答案:

答案 0 :(得分:1)

好的,先测试一下,看看发生了什么:

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

if( !mysql_connect($host, $username, $password) )
  die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
  die( 'Can not select the Database: '.mysql_error() );

$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );

if( !$query )
  die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
  die( 'Query Returned No Records' );

$row = mysql_fetch_array($query);

echo '<pre>';
var_dump( $row );
echo '</pre>';

这应该显示数据库的结果或错误消息。如果您看到错误消息,请更正导致它的任何内容......

以上之后只返回数据库行内容:

$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";

if( !mysql_connect($host, $username, $password) )
  die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
  die( 'Can not select the Database: '.mysql_error() );

$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );

if( !$query )
  die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
  die( 'Query Returned No Records' );

$row = mysql_fetch_array($query);

header( 'Content-type: '.$row['mimetype'] );
echo $row['Image'];

(假设mimetype字段类似于“image / jpg”)

答案 1 :(得分:0)

我建议在输出之前添加内容长度标题:

header("Content-length: " . strlen($content))

答案 2 :(得分:0)

我建议更改标题以使其动态取决于图像mime-type:

header('Content-type: '.$row['mimetype']);