$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. ?>
复制代码
  d  B9 X/ h8 R7 t+ g  j  g1 B

0 `8 u2 M) l+ w# m1. PHP一般数据读取方法:
3 x6 W- q8 N# V' r6 F+ H  u( c
4 P. L7 ]8 u0 \" V4 n
  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. ?>
复制代码

+ d* c! j3 T/ Z7 @, [* S+ j0 q' q( b) ?$ k! z- R( s

0 n" D4 E* `8 N# V( N2 y1 |9 w% e2. PHP一般数据写入方法:! h6 Z/ Y! A; M( p

6 q1 U4 R+ q; t4 U+ z0 G/ G) L6 j8 m; Z* e
  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. ?>
复制代码

% t8 y  c% W& F+ d# q8 z% G" Z, H8 P2 i- p3 @# @

; q3 S: U  M2 t1 Z3 o3、PHP一般数据读取方法:8 h2 K9 J1 P/ q  E9 |7 h
/ y# ^9 I+ b1 b' i0 h, o4 P- y

1 @9 }0 w( j3 W+ h
  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. ?>
复制代码

9 S1 k/ k0 Z) e# ?# a% |1 D
3 ?1 y- Z+ g4 N2 A+ m
6 a7 F5 B( e4 v$ C4、PHP删除数据方法:% W) O9 c' \4 k. M( A
' T, _4 f8 Y( J2 x. z! @7 ^, Z* S
; {! U8 V5 H: 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. ?>
复制代码
" y( r7 s2 d8 F) R
7 W* w! X8 d. F6 p
" u( H" P9 }1 E; u+ K6 u
5、PHP更新数据方法:
; v- z1 P- x: i3 y0 j& U+ u, W9 ^4 ~, y; ^7 D; _& f2 a+ a
  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. ?>
复制代码
1 E& E  l( u4 b2 [" G* `. B  y

& ~4 K3 a. x+ m* W4 d1 z4 Y+ B& U
8 e0 B, Q$ T& {参考: http://blog.csdn.net/goodnew/article/details/19757485

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