$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖
分享到:




[PHP 语言] PHP读取access 数据库的部分方法实例

  1. <?php
  2. //读取mdb数据库例程
  3. $conn = new com("ADODB.Connection");
  4. $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("./SpiderResult.mdb");

  5. $conn->Open($connstr);
  6. $rs = new com("ADODB.RecordSet");
  7. $rs->Open("select * from Content",$conn,1,1);
  8. while(! $rs->eof)
  9. {
  10. echo $rs->Fields(1)->value;
  11. echo "<br>";
  12. echo $rs->Fields(2)->value;
  13. echo "<br>";
  14. echo $rs->Fields(3)->value;
  15. echo "<br>";
  16. echo "<hr>";
  17. $rs->MoveNext();
  18. }
  19. ?>
复制代码
- p4 U2 `9 N' e  l: I9 A5 ]
% A2 O. S, @" w# x$ g" J
1. PHP一般数据读取方法:$ I. k; {% D3 L( z' G2 i
& B) _6 i4 X& j
  1. <?php //读取mdb数据库例程
  2. $conn = new com("ADODB.Connection");
  3. $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
  4. $conn->Open($connstr);
  5. $rs = new com("ADODB.RecordSet");
  6. $rs->Open("select * from userinfo",$conn,1,1);
  7. while(! $rs->eof) {
  8. $f = $rs->Fields(1);
  9. echo $f->value;
  10. echo "  ". $rs->Fields(2)->value;
  11. $rs->MoveNext();
  12. }
  13. ?>
复制代码
5 r/ ?5 a. |3 O# `

. C% j. X% `4 r+ k1 t0 F3 G# i7 E5 k/ J& H  S' h
2. PHP一般数据写入方法:' Y1 o0 `& z" M* T* G; n
$ z+ P/ O$ D4 O9 U1 ~

" ~- W9 E; X9 A) @: s0 Q" M% I
  1. <?php
  2. $connstr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("../chen416/mydata.mdb");
  3. $connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC);
  4. $issuetime=date("Y-m-d H:i:s");
  5. $sql="insert into userinfo (username,userword,email) values('u2','w2','uw@163.com')";
  6. $result=odbc_exec($connid,$sql);
  7. if($result) echo "successful";
  8. else echo "failed";
  9. ?>
复制代码
5 A7 W9 v0 Q$ h2 V! }
, N1 i' h7 k* ~- u
  V7 X' x( ]# e. a+ m' H. v2 g
3、PHP一般数据读取方法:
& A5 ~7 q! k+ I. l! B" a+ ?4 P
( s) D, U& k' v% Y2 [7 r1 U) N- u; Q
  1. <?php //读取mdb数据库例程
  2. $conn=new COM("ADODB.Connection");
  3. $dsn="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=".realpath("../mydata.mdb").";";
  4. //Uid=Admin; Pwd=BDC2002Data;
  5. $conn->open($dsn);
  6. $sql="select * from userinfo";
  7. $rs=$conn->Execute($sql);
  8. $row="0";
  9. while(!$rs->EOF)
  10. {
  11. $string ="";
  12. for ($i=0;$i<$rs->Fields->count;$i++)
  13. {
  14. if ($i==0)
  15. {
  16. $string.=$rs->Fields($i)->Value;
  17. continue;
  18. }
  19. $string.='|'.$rs->Fields($i)->Value;
  20. }
  21. $string.="\n";
  22. if ($row==300)
  23. {
  24. break;
  25. }
  26. $row++;
  27. echo "<br>";
  28. echo $string;
  29. $rs->MoveNext();
  30. }
  31. ?>
复制代码
+ I6 Q* Q! c6 P4 V0 Q+ f+ S1 t9 @
  g7 q/ ?9 h2 E$ c8 P

9 Z) G; x/ j  M0 `4、PHP删除数据方法:
* s+ T* @4 F. {3 X9 w; K( N" _+ r, u2 E% k& L4 u1 _
3 K4 f" Y  D5 ]! d
  1. <?php
  2. $conn = new com("ADODB.Connection");
  3. $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
  4. $conn->Open($connstr);

  5. $query = "delete from userinfo where uid=1";
  6. $result =$conn->Execute($query);

  7. if($result)
  8. echo "删除成功";
  9. else
  10. echo "删除失败";

  11. ?>
复制代码
; ?3 w; |: j3 C$ e2 i

1 S: I* Y4 y2 f# j- s6 }% S4 g  `) p, l7 K1 m1 ?- x& C
5、PHP更新数据方法:6 \7 |! m5 d' A8 e' O1 }+ a) w  l

4 z7 }8 o; D+ @5 A0 `6 f
  1. <?php
  2. $conn = new com("ADODB.Connection");
  3. $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
  4. $conn->Open($connstr);

  5. $query = "update userinfo set username='ganjun' where uid=2";
  6. $result =$conn->Execute($query);

  7. if($result)
  8. echo "更新数据成功";
  9. else
  10. echo "更新数据失败";

  11. ?>
复制代码

  d% W0 z6 H/ F! v3 V
8 P/ y3 c4 B9 D6 H" \$ e
2 ]& Y1 `/ `. K( d) X参考: http://blog.csdn.net/goodnew/article/details/19757485




返回列表
Namecheap
Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting
互联网安全