$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. ?>
复制代码
, H4 l4 r( Z; a/ [1 ]  }% @

8 T; x2 H. G* @# d6 A6 y3 o& k1. PHP一般数据读取方法:& S2 r; [: N- z8 e- B$ z+ j
2 _7 v% o$ M: r
  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. ?>
复制代码

+ g. N' R9 d, m& O6 R( p3 F4 h/ x: _# r

- I2 k7 u7 `' V  W  l2. PHP一般数据写入方法:
5 x9 o7 t$ z. }9 c: ^  a. |
$ j  |, j4 I- i" t  B* B
/ n8 j6 w7 E$ O9 U
  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. ?>
复制代码
0 L# Y8 Y4 E' H0 D9 K9 ]
4 P" O* N$ v9 y* l' D; J6 ?
4 ^3 c, c- y# v: `
3、PHP一般数据读取方法:
" j$ g8 {5 \2 L; S; G- s+ S7 t6 {5 R$ \: ?

1 s9 [1 B" ~; Z% t) {( f
  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. ?>
复制代码

5 Y" {. E3 D# V( P- {! Z* B* l* {. s  o6 l- i5 H  t) y' X4 u

% ~+ \( N7 a: Y2 ~4、PHP删除数据方法:" |0 Q# d! l+ M: s/ O/ s1 b) @
# ^7 n1 N% r# P- L' F4 R/ B. Q

, v0 G, j; E% }3 t1 |4 g
  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. ?>
复制代码

) E0 L* ^8 \& k# j7 [/ d
1 Z# o5 f9 S5 w& R% n/ Z& p  S4 t7 C- l$ w) K  n: z7 y6 s; {
5、PHP更新数据方法:
5 n1 v* g+ O' j% W' R% A& k9 @1 ^* H$ u
% K% t7 N. C6 |) s1 v5 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 = "update userinfo set username='ganjun' where uid=2";
  6. $result =$conn->Execute($query);

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

  11. ?>
复制代码

4 s. n4 M! b5 P: s) U; o
( ^9 y, s% w& \5 Z0 A( {6 ?: V' [: O) ~; \: S$ w3 m
参考: http://blog.csdn.net/goodnew/article/details/19757485

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