$query = "SELECT pic FROM tablename WHERE id = $id"; if (!($result = @ mysql_query ($query,$connection)))
showerror(); $data = @ mysql_fetch_array($result); if (!empty($data["pic"]))
{
// Output the GIF MIME header
header("Content-Type: image/gif");
// Output the image
echo $data["pic"];
}
showerror(); $data = @ mysql_fetch_array($result); if (!empty($data["pic"]))
{
// Output the GIF MIME header
header("Content-Type: image/gif");
// Output the image
echo $data["pic"];
}
建议还取一个字段存放图片类型的!
谢谢高手,我正好在想做这个呢!没有具体的代码自己写总是忐忑……
不知道自己那样写对不对,总算有个参考!
应该说是规范呵呵
//UP_PIC.PHP
<?php
if($submit){
$host="localhost";
$sa="root";
$pw="";
mysql_connect($host,$sa,$pw);
mysql_select_db("bin_data");
//addslashes()
$fp=fopen($upfile,"rb");
if(!fp)
{ echo "Error Opening File!" ; }
else
{ $buf=addslashes(fread($fp,$upfile_size));
$sqlstr="insert into binary_data (description,pic,filename,filesize,filetype)".
"values ('$fdescription','$buf','$upfile_name','$upfile_size','$upfile_type')";
$result=mysql_query($sqlstr);
$id=mysql_insert_id();
print "<p> This file has the following Database ID:<b>$id</b>";
}
mysql_close();
}
else
{
?>
<form method="post" action="up_pic.php" enctype="multipart/form-data">
<hr>
File Description:<br>
<input type="text" name="fdescription" size="40">
<br>
File to upload/store in database:<br>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="upfile" size="40">
<hr>
<p> <input type="submit" name="submit" value="submit"></p>
</form>
<?php
}
?>
而图片显示代码分两个程序一个为GET_PIC.PHP,一个为GET_PIC_.PHP
//GET_PIC.PHP
<?php
$host="localhost";
$sa="root";
$pw="";
@mysql_connect($host,$sa,$pw) or die("Unable to connect to mySQL server");
@mysql_select_db("bin_data") or die("Unable to select database");
$query="select id,description,pic,filetype from binary_data"; //where id=5";
$result=@mysql_query($query) or die("Can not Perform Query");
While($arow=mysql_fetch_object($result))
{
echo "<IMG SRC=\"get_pic_.php?id=$arow->id\">";
}
mysql_close();
?>
//GET_PIC_.PHP
<?php
$query_str="select id,description,pic,filetype from binary_data where id=$id";
$result=@mysql_query($query_str) or die("Can not Perform Query");
$a_row=mysql_fetch_object($result);
if (!empty($a_row["pic"]))
{
// Output the GIF MIME header
header("Content-Type: image/gif");
// Output the image
echo $a_row["pic"];
}
//Header( "Content-type: image/gif");
//echo $arow->pic; ?> 可就是无法在WEB页面上显示数据库中存储的图片,请各位行家分析我的问题出在哪里?先行谢过!
mysql_fetch_object($result);
来取结果是得到一个对象.不是数组呀!应该这样if (!empty($a_row->pic))
{
// Output the GIF MIME header
header("Content-Type: image/gif");
// Output the image
echo $a_row->pic;
}
我认为关键问题不在是对象还是数组,我照您的试了,还是照旧不显示图片。
是不是跟PHP.INI中的某些配置项有关呢?
我设置了其中的"extension_dir=d:\php5\ext" 和 "extension=php_gd2.dll",
还有"extension=php_mysql.dll",是否还有一些设置没有到位呢?
你PIC字段是什么类型的字段??
应该没什么关系可能是的确字段不对还有就是GET_PIC_.PHP用来显示图片的那个文件,我也建立了mysql连接的
你试着把那个文件也加上mysql连接看看同是菜鸟
就前天回你的帖子的时候刚搞好图片显示的问题,祝你好运……
如果用数据库或者图片重新生成图片然后显示,:
显示页面就打个框架,在要显示图片的地方<img src="xxx.php">
//xxx.php 为生成图片的程序可以带个参数?num=xxx控制想要的图片
******************************************在xxx.php文件中:格式如下:header()读取数据,创建新图片。imagexxx()**************************************直接输出数据库的二进制图片: echo $pic_blob;**************************************控制图片显示大小:<img width="" height="">**************************************ps:俺本身就是菜鸟。大大们指正
代码如下。
<?
include "conect.inc.php";
$query="select * from image";
$result=mysql_query($query,$conn) or die(mysql_error);
while($row=mysql_fetch_array($result))
{
echo $row["pic"];
echo "<br>";}
?>
这是这个文件所有的代码。我不知道这是怎么回事。没有header(……)函数,并且gif和jpg都能显示。
我是在xp+iis下面。
**************
list_echo.php
**************<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>LIST MINIATURE</title>
</head><body>
<table width="768" height="768" border="1" align="center" bordercolor="#44CC88"><?
if(!$page){
$page = 1;
}for($i=0;$i<3;$i++){
echo "<tr>";
for($j=0;$j<3;$j++){
$k = ($i*3) +$j+(($page-1)*9);
echo "<td height=\"256\" width=\"256\" align=\"center\" valign=\"middle\" bgcolor=\"#AAAAAA\">";
echo "<img src=list_echo_getdata.php?k=".$k;
echo "></td>";
}
echo "</tr>";
}
?></table></body>
</html>
**********************
list_echo_getdata.php
**********************<?
include("config/conn.php");$result = mysql_query(" SELECT * FROM ".PIC_TABLE." ORDER BY CreateTime LIMIT ".$k.",1", $DB_CONN);
$array = mysql_fetch_array($result);
echo $array['Pic'];
?>