PHP从MySQL中读Gif图片数据时出现乱码,请指教错误原因

iCasino 2003-12-03 11:29:20
我想在MySQL中存取图片,数据库名为binary_data,表名binary_data,bin_data是LongBlob字段,id=2是一幅Gif
图片,已存进了MySQL
我运行以上程序,却出现以下错误,图片是乱码,无法正确显示,请多指教。

我使用的是WinXp+php-4.3.4-win+IIS 5.0+mysql-4.0.16-win

错误提示:
Connected successfully Warning: Cannot modify header information - headers already sent by

(output started at F:\software\PHP\display.php:5) in F:\software\PHP\display.php on line 10

GIF89a??????????????... //ʽ¾ݿ≺³ɽű
为方便您跟踪问题,我把数据库结构生成脚本及图片插入脚本贴在了下面。
//数据库结构生成脚本
/*==============================================================*/
/* Database name: binary_data */
/* DBMS name: mysql-4.0.16-win */
/* Created on: 2003-12-3 10:52:57 */
/*==============================================================*/


drop index "PRIMARY" on binary_data;

drop table if exists binary_data;

/*==============================================================*/
/* Table: binary_data */
/*==============================================================*/
create table if not exists binary_data
(
id integer(4),
bin_data blob,
filetype varchar(100),
filename varchar(50),
primary key (id)
);

/*==============================================================*/
/* Index: "PRIMARY" */
/*==============================================================*/
create unique index "PRIMARY" on binary_data
(
id
);


//图片插入脚本
<?php
/* 连接选择数据库 */
$link = mysql_connect("localhost", "root", "")
or die("Could not connect : " . mysql_error());
print "Connected successfully";
mysql_select_db("binary_data") or die("Could not select database");
$file = "c:\\image.gif";
$data=addslashes(fread(fopen($file,"rb"),filesize($file)));
$result=MYSQL_QUERY("INSERT INTO binary_data(bin_data,filename) values('$data','$file')");
$id=MYSQL_insert_id();
print "This file has the following id: $id";
mysql_free_result($result);
/* 断开连接 */
mysql_close($link);
?>
...全文
194 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2003-12-04
  • 打赏
  • 举报
回复
把print "Connected successfully";去掉,这个串不是图片内容,若作为图片解释自然就是乱码了
header ("Content-type: image/gif");
这个头还是要的,他通知浏览器将要传送的数据是什么类型的
iCasino 2003-12-04
  • 打赏
  • 举报
回复
大哥,我想结贴给分,但现在系统好象连结贴的连接都不见了,过天一定把分奉上。再次感谢!
iCasino 2003-12-04
  • 打赏
  • 举报
回复
果然是这样,我用来测试连接的串坏我的事了。谢谢!
iCasino 2003-12-03
  • 打赏
  • 举报
回复
对不起,显示的脚本忘记贴了:
<?php
/* 连接选择数据库 */
$link = mysql_connect("localhost", "root", "")
or die("Could not connect : " . mysql_error());
print "Connected successfully";
mysql_select_db("binary_data") or die("Could not select database");
$query="select bin_data from binary_data where id=2";
$result=@MYSQL_QUERY($query);
$data=@MYSQL_RESULT($result,0,"bin_data");
header ("Content-type: image/gif");
echo $data;
mysql_close($link);
?>
xuzuning 2003-12-03
  • 打赏
  • 举报
回复
(output started at F:\software\PHP\display.php:5) in F:\software\PHP\display.php on line 10
问题已经很清楚了,display.php第5行处有输出,请贴出display.php前5行的代码
iCasino 2003-12-03
  • 打赏
  • 举报
回复
我将 // header ("Content-type: image/gif");
注释掉,没有了错误提示,但输出仍是乱码,显示如下Connected successfullyGIF89a––ÔÿÿÿÌÿÿÿÌÿÿÌÌÌÌÌÌ™Ìÿ™Ìÿ™™Ì™™Ìf™ÌfÌ™f™™™™™™f
那么,是不是用echo语句输出的只是二进制流呢?需要<img src=" ">之类的语句吗?

<?php
/* 连接选择数据库 */
$link = mysql_connect("localhost","root", "")
or die("Could not connect : " . mysql_error());
print "Connected successfully";
mysql_select_db("binary_data") or die("Could not select database");
$query="select bin_data from binary_data where id=2";
$result=@MYSQL_QUERY($query);
$data=@MYSQL_RESULT($result,0,"bin_data");
// header ("Content-type: image/gif");
echo $data;
mysql_close($link);
?>

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧