$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. ?>
复制代码

& ?( c4 m& ~, H$ b
3 a+ i! E  N  a6 ^/ k! ~2 W1. PHP一般数据读取方法:
! X0 [6 }+ s! w4 ~' R0 J/ Q; S  C1 n6 g6 Z( i5 H( G) |' X2 A
  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. ?>
复制代码
  N, ^( T. B3 j: O$ B( z8 q

7 l3 [0 F, Y0 r6 F' B3 S- L9 c5 O
/ x7 d- W4 i% n+ a2 S2. PHP一般数据写入方法:2 \! t! Y, C. U1 n. F& C

0 r3 l% ]1 n- G3 ^5 y
: e& R: R# n8 W. Q$ g$ Z# t& s
  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. ?>
复制代码

+ x& t: P0 D- Z
& r4 J# y* V) S4 C- f& K  I( O- E; K& b: i1 g# V* {3 c9 e
3、PHP一般数据读取方法:
  Q1 \  ?, p9 ]! o. H# Z9 v( v. g( v3 s. H! U& W( H

* O/ R! y$ B! R# e+ \" D
  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. ?>
复制代码
! p! c( w' h1 P# S; ?
# `( x9 B" d; R) Y1 K7 W

2 H, @' o% l6 T' C4、PHP删除数据方法:
0 ]9 m3 H1 Z8 P% Z3 H# W( q# {8 J  k/ i0 g5 c* ~4 O7 m
1 D: q& o7 V$ R: b  _2 p5 W, }
  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. ?>
复制代码
. I# h% V6 P# a2 R  _' N

- M# _4 o+ S- _. o4 M& ]! L
6 Z3 j/ p* J  ~# P6 E2 z- Q5、PHP更新数据方法:
, X& j! t7 d  I; L
; n4 z) D8 ]: N. }7 W, ]( T
  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. ?>
复制代码

+ w* o2 }) S5 Q" B6 L, g9 a7 F# ?" W: R1 D* w8 {9 L: z' U! [' B, {

8 O& G7 u  E0 M4 g2 D" e参考: http://blog.csdn.net/goodnew/article/details/19757485

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