无法在 PHP 中显示图像

发布于 2024-12-06 19:34:43 字数 626 浏览 1 评论 0原文

这是我的图像显示代码 -

$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

here's my code for image display -

$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;

This is the error i'm getting

The image “http://www....” cannot be displayed because it contains errors.

what is wrong? The datatype of field in mysql is mediumblob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-12-13 19:34:43

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

$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”)

OK, first test, to see what is happening:

$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>';

That should either show you the results from the database, or an error message. If you see an error message, correct whatever is causing it...

After the above just returns the Database row contents:

$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'];

(assuming that the mimetype field is something like "image/jpg")

层林尽染 2024-12-13 19:34:43

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

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

I'd suggest adding a content-length header before your output:

header("Content-length: " . strlen($content))
娇纵 2024-12-13 19:34:43

我建议更改标头以使其动态取决于图像 mime 类型:

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

I'd suggest to change the header to make it dynamic depend on the image mime-type:

header('Content-type: '.$row['mimetype']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文