MYSQL BLOB存放的图片在页面中显示乱码

Big鹏 2010-07-28 03:53:53
想取出存在数据库表中的BLOB字段的图片,在页面中显示出来.
代码如下:
<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HEAD>
<body>
<?php
$host="localhost";
$user="root";
$password="123456";
$database="test1";
$table_name="teacher";
$conn=mysql_connect($host,$user,$password);
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
mysql_query("SET NAMES 'GB2312'");
$v_find="select pic from teacher";
$result=mysql_query($v_find,$conn) ;

while($row=mysql_fetch_object($result))
{
echo "<img src='".$row->pic."'>";
}
?>
</body>
</html>
结果:显示出来异常
...全文
554 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhtor 2010-09-25
  • 打赏
  • 举报
回复
搞定了:

<?php

if($id)
{
@mysql_connect("localhost","root","");
@mysql_select_db("picdb");

$query = "select pic,fileType from pics where id=$id";
$result = @mysql_query($query);

$data = @mysql_result($result,0,"pic");
$type = @mysql_result($result,0,"fileType");

header("content-type:$type");
echo $data;
}

?>

假如本文档的名字是 picShow.php
假如图片在表里的ID号为 1 ,则地址为
http://localhost/picShow.php?id=1
zhtor 2010-09-25
  • 打赏
  • 举报
回复
楼主,问题解决了吗?我也遇到同样的问题了,国内国外网站找了个遍,无解啊……
xuzuning 2010-07-29
  • 打赏
  • 举报
回复
pic.php
<?php
$host="localhost";
$user="root";
$password="123456";
$database="teacher";
$table_name="student";
$conn=mysql_connect($host,$user,$password);
echo "1111";
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
// mysql_query("SET NAMES 'GB2312'"); //blob 类型是二进制的,不需要设置语言编码
$result=mysql_query("select * from student where picnum=$_GET[picnum]");
$row=mysql_fetch_object($result);
Header("Content-type:image/gif");
echo $row->pic;
?>

主程序
<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HEAD>
<body>
<?php
$host="localhost";
$user="root";
$password="123456";
$database="test1";
$table_name="teacher";
$conn=mysql_connect($host,$user,$password);
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
mysql_query("SET NAMES 'GB2312'");
$v_find="select picnum from teacher";
$result=mysql_query($v_find,$conn) ;

while($row=mysql_fetch_object($result))
{
echo "<img src='pic.php?picnum=".$row->picnum."'>";
}
?>
</body>
</html>
Dleno 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 amani11 的回复:]

数据库编码utf8,前台"SET NAMES 'GB2312'",后果不可预计


echo "1111";//这一行干吗呢?
[/Quote]
++

数据库编码utf8,程序"SET NAMES 'utf8'"

你这里不能输出任何与图象数据无关的字符。
否则浏览器无法解析你的图片
amani11 2010-07-29
  • 打赏
  • 举报
回复
数据库编码utf8,前台"SET NAMES 'GB2312'",后果不可预计


echo "1111";//这一行干吗呢?
Big鹏 2010-07-29
  • 打赏
  • 举报
回复
修改后的pic.php
<?php
$host="localhost";
$user="root";
$password="123456";
$database="teacher";
$table_name="student";
$conn=mysql_connect($host,$user,$password);
echo "1111";
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
mysql_query("SET NAMES 'GB2312'");
$result=mysql_query("select * from student where picnum=123");
$row=mysql_fetch_object($result);
Header("Content-type:image/gif");
echo $row->pic;
?>
我在执行的时候,就是没有跳转到pic.php.
Content-type我是在程序中这样设置的 Header("Content-type:image/gif");
还是只显示一个小叉号
数据库如下

--
-- 表的结构 `student`
--

CREATE TABLE `student` (
`picnum` int(11) NOT NULL,
`pic` blob NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-- 导出表中的数据 `student`


高手指点!!!万分感激.
amani11 2010-07-29
  • 打赏
  • 举报
回复
pic.php



连接数据库了么。查看下输出了什么
myhope88 2010-07-29
  • 打赏
  • 举报
回复
你数据库存放的又不是路径不能这样子弄吧,应该让它转到另一个php页面,然后根据参数获取数据库中的数据,将数据输出来,不过得记得将content-type设图片显示形式的
Big鹏 2010-07-29
  • 打赏
  • 举报
回复
我的blob字段存放不是图片路径,是图片的二进制流.我按您的提示,改进如下,
<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HEAD>
<body>
<?php
$host="localhost";
$user="root";
$password="123456";
$database="teacher";
$table_name="student";
$conn=mysql_connect($host,$user,$password);
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
mysql_query("SET NAMES 'GB2312'");
$v_find="select * from student";
$result=mysql_query($v_find,$conn) ;
while($row=mysql_fetch_object($result))
{
echo "$row->picnum";//picnum=123;
echo "<img src=\"pic.php?picnum=$row->picnum\">";

}
?>
</body>
</html>
代码:pic.php

<?php

$result=mysql_query("select * from student where picnum=$picnum").or die("can't perform query");
$row=mysql_fetch_object($result);
Header("Content-type:image/gif");
echo $row->pic;
?>

修改后,根据没有跳到pic.php文件当,所以图片没有显示出来,只显示一个小叉号.
xuzuning 2010-07-29
  • 打赏
  • 举报
回复
假定你表中有 picnum = 123 的记录

请直接执行
http://localhost/pit.php?picnum=123

看结果
Big鹏 2010-07-29
  • 打赏
  • 举报
回复
71.php
代码:<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HEAD>
<body>
<?php
$host="localhost";
$user="root";
$password="123456";
$database="teacher";
$table_name="student";
$conn=mysql_connect($host,$user,$password);
if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);
$v_find="select picnum from student";
$result=mysql_query($v_find,$conn) ;

while($row=mysql_fetch_object($result))
{
echo "<img src='pic.php?picnum=".$row->picnum."'>";
}
?>
</body>
</html>

pic.php 代码:
<?php
$host="localhost";
$user="root";
$password="123456";
$database="teacher";
$table_name="student";
$conn=mysql_connect($host,$user,$password);

if(!mysql_connect($host,$user,$password))
die("connect erroring..... ".mysql_error());
mysql_select_db($database,$conn);

$result=mysql_query("select * from student where picnum=$_GET[picnum]");
$row=mysql_fetch_object($result);
Header("Content-type:image/gif");
echo $row->pic;
?>

页面还是显示一个小红错号,而且也没有跳至pic.php
数据库


CREATE TABLE `student` (
`picnum` int(11) default NULL,
`pic` blob NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

帮帮忙吧! 谢谢
Dleno 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 amani11 的回复:]

引用的图片,单独写一个文件

比如<img src="picture.php?id=123">

在picture.php文件,根据参数值,链接数据库,读取字段信息,echo出来
[/Quote]

++

picture.php,只输出图片内容,其他什么都不要输出。
amani11 2010-07-28
  • 打赏
  • 举报
回复
引用的图片,单独写一个文件

比如<img src="picture.php?id=123">

在picture.php文件,根据参数值,链接数据库,读取字段信息,echo出来
CunningBoy 2010-07-28
  • 打赏
  • 举报
回复
你数据库里面存的是图片的路径吗?

21,886

社区成员

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

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