想取出存在数据库表中的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>
结果:显示出来异常
代码如下:
<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>
结果:显示出来异常
++picture.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);
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文件当,所以图片没有显示出来,只显示一个小叉号.
<?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`
高手指点!!!万分感激.
echo "1111";//这一行干吗呢?
否则浏览器无法解析你的图片
<?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>
代码:<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;帮帮忙吧! 谢谢
http://localhost/pit.php?picnum=123看结果
{
@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