通行证: 用户 密码 域名空间  下载中心 社区论坛 信息公告 my小屋
联系我们
设为首页
加入收藏

 

qq,asp,php,jsp,xml,sql,.net,编程 程序 网页图象 建站经验 私服
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿
论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图
专题 | 应用软件 | 聊天通讯 | q q 专栏 | 建站经验 | 在线工具 | 站长club | 注 册 表 | 旧版
社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务
当前位置:首页>>在线服务>>疑难解答>>正文 新版上线![旧版]
注:打开慢时请稍等

怎么将图片存入mysql数据库?

http://www.iyit.net  日期:2006-10-6 15:39:50  来源:iyit.net收集  点击:
参加讨论
怎么将图片存入mysql数据库?  
---------------------------------------------------------------  
 
贴一篇文章你自己看一下吧。  
 
怎样插入图象进mysql?  
发信站:  bbs  水木清华站  (thu  jul  15  13:25:46  1999)  www-post  
 
【  在  weffen  (wef)  的大作中提到:  】  
:  craete  table  pic(intro  text,image  longblob)  
:  sql    语句怎么写?  
这是php+mysql的例子,应该满足你的要求吧.  
saving  images  in  mysql    
 
sometimes,  it's  more  convenient  to  save  images  in  a  database  than  as  files.    
mysql  and  php  make  it  very  easy  to  do  this.  in  this  article,  i  will  describe    
how  to  save  images  in  a  mysql  database  and  display  them  later  on.    
 
   setting  up  the  database    
 
 
the  difference  between  any  regular  text  or  integer  fields  and  a  field  that    
needs  to  save  an  image  is  the  amount  of  data  that  is  needed  to  be  held  in  the    
field.  mysql  uses  special  fields  to  hold  large  amounts  of  data.  these  fields    
are  known  as  blobs  (blob).    
 
here  is  the  blob  definition  from  the  mysql  site  :    
 
a  blob  is  a  binary  large  object  that  can  hold  a  variable  amount  of  data.  the    
four  blob  types  tinyblob,  blob,  mediumblob  and  longblob  differ  only  in  the    
maximum  length  of  the  values  they  can  hold    
 
 
 
for  more  information  about  mysql  blobs  check  out  http://www.mysql.net/manual_c  
hapter/manual_reference.html#blob  
 
use  the  next  syntax  to  create  a  basic  table  that  will  hold  the  images:  
             
           create  table  images  (  
                       picnum  int  not  null  auto_increment  primary  key,  
                       image  blob  
           );  
   
   setting  the  upload  script    
 
 
an  example  of  a  file  upload  front  end  can  be  seen  at  file  uploading  by  berber    
(29/06/99).  what  we  need  now  is  the  php  script  that  will  get  the  file  and    
insert  it  into  mysql.  the  next  script  does  just  that.  in  the  script,  i'm    
assuming  that  the  name  of  the  file  field  is  "picture".    
 
 
<?  
if($picture  !=  "none")  {  
           $psize  =  filesize($picture);  
           $mysqlpicture  =  addslashes(fread(fopen($picture,  "r"),  $psize));  
           unlink($picture);  
           mysql_connect($host,$username,$password)    
                       or  die("unable  to  connect  to  sql  server");  
           @mysql_select_db($db)    
                       or  die("unable  to  select  database");  
           mysql_query("insert  into  images  (image)  values  '($mysqlpicture')")  
                       or  die("can't  perform  query");  
}  
else  {  
           echo"you  did  not  upload  any  picture";  
}  
?>  
 
this  is  all  that  is  needed  to  enter  the  image  into  the  database.  note  that  in    
some  cases  you  might  get  an  error  when  you  try  to  insert  the  image  into    
mysql.  in  such  a  case  you  should  check  the  maximum  packet  size  allowed  by    
your  mysql  ver.  it  might  be  too  small  and  you  will  see  an  error  about  this  in    
the  mysql  error  log.    
 
what  we  did  in  the  above  file  is  :  
 
1.  check  if  a  file  was  uploaded  with  if($picture  !=  "none").  
2.  addslashes()  to  the  picture  stream  to  avoide  errors  in  mysql.  
3.  delete  the  temporary  file.  
3.  connect  to  mysql,  choose  the  database  and  insert  the  image.  
 
 
 
   
   displaying  the  images    
     
 
now  that  we  know  how  to  get  the  images  into  the  database  we  need  to  figure    
out  how  to  get  them  out  and  display  them.  this  is  more  complicated  than    
getting  them  in  but  if  you  follow  these  steps  you  will  have  this  up  and    
running  in  no  time.    
 
since  showing  a  picture  requires  a  header  to  be  sent,  we  seem  to  be  in  an    
impossible  situation  in  which  we  can  only  show  one  picture  and  than  we  can't    
show  anymore  since  once  the  headers  are  sent  we  can't  send  any  more  headers.    
 
 
this  is  the  tricky  part.  to  outsmart  the  system  we  use  two  files.  the  first    
file  is  the  html  template  that  knows  where  we  want  to  display  the  image(s).    
it's  a  regular  php  file,  which  builds  the  html  that  contains  the  <img>  tags,    
as  we  want  to  display  them.  the  second  file  is  called  to  provide  the  actual    
file  stream  from  the  database  directly  into  the  src  property  of  the  <img>    
tag.    
 
this  is  how  a  simple  script  of  the  first  type  should  look  like:    
 
 
 
 
<html>  
<body>  
<?  
           mysql_connect($host,$username,$password)    
                       or  die("unable  to  connect  to  sql  server");  
           @mysql_select_db($db)    
                       or  die("unable  to  select  database");  
           mysql_query("select  *  from  images")  
                       or  die("can't  perform  query");  
           while($row=mysql_fetch_object($result))  {  
                       echo  "<img  src=\"secondtype.php3?picnum=$row->picnum\">";  
           }  
?>  
</body>  
</html>  
 
 
 
while  the  html  is  being  displayed,  the  secondtype.php3  file  is  called  for    
each  image  we  want  to  display.  the  script  is  called  with  the  picture  id    
(picnum)  which  allows  us  to  fetch  the  image  and  display  it.    
 
the  secondtype.php3  file  looks  like  this  :    
 
 
 
 
<?  
$result=mysql_query("select  *  from  images  where  picnum=$picnum")    
           or  die("can't  perform  query");  
$row=mysql_fetch_object($result);  
header(  "content-type:  image/gif");  
echo  $row->image;  
?>  
 
 
this  is  the  whole  theory  behind  images  and  mysql.  the  scripts  in  this  example    
are  the  basics.  you  can  now  enhance  these  scripts  to  include  thumbnails,  set    
the  images  in  various  positions,  enhance  the  database  table  to  hold  an  alt    
field,  check  the  width  and  height  of  the  images  before  you  insert  them  into    
the  database  and  keep  that  data  in  the  table  too  etc...    
   
 
--  
※  来源:·bbs  水木清华站  bbs.net.tsinghua.edu.cn·[from:  202.117.17.24]    


编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部]
上一篇:怎样让输入的文章内容折行?
下一篇:如何实现从一个页面自动导向另外一个页面???
转载请注明来源:www.iyit.net
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

 相关文章
我在windows  xp上安装apache2.044+php 我只是想在本地机上学习php ,还要安装 怎么删除mysql???
怎么样修改mysql安装后的root空密码? 在win2k下如何安装apache、mysql、php? php接受sqlserver存储过程的返回值问题
mysql下的全文检索代码 php+sqlserver分页最简单方法 一个困扰我的问题,mysql的时间问题。
请问在mysql中如何生成临时表哪? 一个简单的mysql数据库分页的程序模板( mysql数据库区分大小怎么解决?
如何再mysql中存储数组? 使用php和mysql保存和显示图片的全过程 php将mysql中jpeg图片取出后如何直接缩
把附件(word、excel、pdf)或图片存贮 如何将mysql中的数据导入到excel中?? winodws下iis/apache+php+mysql的安装配
使用apache 2和mysql 4.1.3安装php 5.0 iis与sql服务器安全加固详解 远程连接mysql资料
mysql密码忘记的修改 mysql初学者使用指南 mysql数据库连接过多的错误,可能的原因
最新更新 热点排行 推荐新闻
我在windows  xp上安装apache2.044+ph
apache+php,怎样打开session支持??
在linux中怎么配置pdflib和php  高分!
怎么样同时解释.php和.php3的文件呀,
php 4.1.0 及以后版本使用post变量的接
我的qq被盗走的原因分析
webqq好玩不?大鸟带你一起体验
奇奇怪怪的qq密技十五招
qq被盗到出售过程详解
微软winxp sp3再次跳票 推至08年发布
google苹果合作浏览器 防恶意网站
微软操作系统实现开源?
网管应当如何管理windows操作系统?
让windows xp系统锁定期间拒绝关机
windowsvista中文版11月30日正式发布
我在windows  xp上安装apache2.044+ph
php 在linux 下如何開啟ftp功能
apache2-win32+php的成功安装方法
怎么删除mysql???
apache+php,怎样打开session支持??
合并vcd片断、快速删除文件夹--dos命令
dos教程 dos命令基础应用
破解qq密码如此简单 
低格、分区、高格的应对--dos命令应用
一劳永逸--批处理命令(一)
qq2006 beta3隆重发布 实用功能一一奉
美国微软总部相中重庆15岁网络奇才(图
qq号码激活的常见问题及案例分析 
sql server安装文件挂起错误解决办法
三分钟让你的系统变处女:acronis tru
exeplorer.exe错误的问题的总结、解决
我的qq被盗走的原因分析
webqq好玩不?大鸟带你一起体验
奇奇怪怪的qq密技十五招
qq被盗到出售过程详解
google苹果合作浏览器 防恶意网站
网管应当如何管理windows操作系统?
让windows xp系统锁定期间拒绝关机
windowsvista中文版11月30日正式发布
google优化网站管理员指导方针
hilltop算法- 探索google排名新算法

设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接
copyrights © 2004-2006 iyit.net all rights reserved.
网站合作、广告联系qq:147007642、466949678
易特网络技术 点击这里给我发消息