PHP ACCESS连接类-1

本文介绍了一个用于操作Access数据库的PHP类,该类提供了数据写入、更新、删除和查询的功能,并支持分页显示查询结果。通过实例演示了如何使用这个类进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <?
  2. /*===========================================================================================================================
  3.     类名:mdbClass;
  4.     作用:操作access的DB类;
  5.     作者:biyuan(6010707);
  6.     使用方法及范例:
  7.         $db = new mdbClass();    //实例
  8.         $db->tabName = "gbook";    //指定要操作的表名
  9.         //数据写入操作
  10.         $db->aryChar = "g_name , g_mail , g_oicq , g_tel , g_img , g_body";
  11.         $db->aryText = "'admin' , 'xxxx@163.com' , 6010707 , '15994275xxx' , '/face/1.gif' , '测试数据'";
  12.         $db->setData();
  13.         //数据更新操作
  14.         $db->aryChar = "g_name = 'scriptcn' , g_tel = '13800138xxx'";
  15.         $db->aryText = "1 = 1";        //条件;赋予1=1表示批量操作
  16.         $db->upData();
  17.         //数据删除操作
  18.         $db->aryText = "id = 4";
  19.         $db->delData();
  20.         //数据查询操作
  21.         $db->aryChar = "id , g_name , g_mail , g_oicq , g_time , g_body";    //可使用“*”全部查询
  22.         $db->aryText = "1 = 1 order by id";    //条件1 = 1表示查询所有记录
  23.         $db->pageView = true;            //是否分页
  24.         $db->pageSize = 10;            //指定每页记录数
  25.         $db->pageUrl = "?id=1&";        //传入其它保留参数
  26.         $db->pageCode = $_GET['page'];        //获取当前页序号
  27.         $db->getData();
  28.         echo "<table border='1'>/n";
  29.         for($i = 0 ; $i < count($db->bodyAry) ; $i ++){    //注意这里,$db->bodyAry是一个二维数组,行 -> 列
  30.             echo "<tr>/n";
  31.             for($j = 0 ; $j < count($db->bodyAry[$i]) ; $j ++){
  32.                 echo "<td>" . $db->bodyAry[$i][$j] . "</td>/n";
  33.             }
  34.             echo "</tr>/n";
  35.         }
  36.         if($db->pageView == true){
  37.             echo "<tr>/n<td colspan='7'>";
  38.             echo $db->pageViewText;        //这里是DB类返回的分页字符
  39.             echo "</td>/n</tr>/n</table>";
  40.         }
  41.         //echo $db->showMessage;    //返回提示内容,调试时可开启
  42. ===========================================================================================================================
  43. */
  44. class mdbClass {
  45.     var $dbPath = 'gbook.mdb';    //数据库路径
  46.     var $tabName;            //表名
  47.     var $aryChar;            //写入、查询操作时为列的集合,更新操作时为更新具体内容
  48.     var $aryText;            //写入操作时为值的集合,更新、删除、查询操作时为更新的条件,批量请赋予1=1
  49.     var $showMessage;        //操作返回的提示
  50.     var $pageCode = 1;        //当前页,程序默认为1
  51.     var $pageSize = 10;        //每页显示记录数,程序默认为10
  52.     var $pageUrl = '?';        //分页时传入的其它保留参数
  53.     var $pageViewText;        //输出分页字符串
  54.     var $pageView = false;        //是否显示分页,默认为不显示
  55.     var $bodyAry = Array();        //返回查询的数据
  56.     var $siteCode = Array(        //返回提示的文字,目的:多语言
  57.         0 => '数据库连接成功!',
  58.         1 => '数据库连接失败!',
  59.         2 => '数据写入成功!',
  60.         3 => '数据更新成功!',
  61.         4 => '数据删除成功!',
  62.         5 => '数据查询失败!',
  63.         6 => '首页',
  64.         7 => '上一页',
  65.         8 => '下一页',
  66.         9 => '尾页'
  67.     );
  68.     //数据库连接
  69.     function conn(){
  70.         try {
  71.             $this->conn = new com("ADODB.Connection");
  72.             $this->conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->dbPath));
  73.             $this->showMessage = $this->siteCode[0];
  74.         }
  75.         catch(Exception $e){
  76.             $this->showMessage = $e->getMessage() . '<br />' . $this->siteCode[1];
  77.         }
  78.     }
  79.     //数据库关闭
  80.     function conn_close(){
  81.         $this->conn->close();
  82.     }
  83.     //写入数据
  84.     function setData(){
  85.         $this->conn();
  86.         $this->conn->execute("insert into $this->tabName($this->aryChar) values($this->aryText)");
  87.         $this->showMessage = $this->siteCode[2];
  88.         $this->conn_close();
  89.     }
  90.     //更新数据
  91.     function upData(){
  92.         $this->conn();
  93.         $this->conn->execute("update $this->tabName set $this->aryChar where $this->aryText");
  94.         $this->showMessage = $this->siteCode[3];
  95.         $this->conn_close();
  96.     }
  97.     //删除数据
  98.     function delData(){
  99.         $this->conn();
  100.         $this->conn->execute("delete from $this->tabName where $this->aryText");
  101.         $this->showMessage = $this->siteCode[4];
  102.         $this->conn_close();
  103.     }
  104.     //查询数据
  105.     function getData(){
  106.         $this->conn();
  107.         $rs = $this->conn->execute("select $this->aryChar from $this->tabName where $this->aryText");
  108.         if(!$rs->Eof){
  109.             $j = 0;
  110.             $k = 0;
  111.             if(!preg_match("/^/d+$/" , $this->pageCode)){
  112.                 $this->pageCode = 1;
  113.             }
  114.             while(!$rs->Eof){
  115.                 $j ++;
  116.                 if(($j > ($this->pageCode - 1) * $this->pageSize) && ($j <= $this->pageCode * $this->pageSize)){
  117.                     for($i = 0 ; $i < $rs->Fields->count ; $i ++){
  118.                         $this->bodyAry[$k][] = $rs->Fields[$i]->value;
  119.                     }
  120.                     $k ++;
  121.                 }
  122.                 $rs->movenext();
  123.             }
  124.             //分页
  125.             if($this->pageView == true){
  126.                 $this->pageViewText = '[' . $j . '][' . $this->pageCode . '/' . ceil($j / $this->pageSize) . '] ';
  127.                 if($j > $this->pageSize){
  128.                     if($this->pageCode > 1){
  129.                         $this->pageViewText .= "<a href='" . $this->pageUrl . "page=1'>" . $this->siteCode[6] . "</a> ";
  130.                         $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ($this->pageCode - 1) . "'>" . $this->siteCode[7] . "</a> ";
  131.                     }
  132.                     else {
  133.                         $this->pageViewText .= $this->siteCode[6] . " ";
  134.                         $this->pageViewText .= $this->siteCode[7] . " ";;
  135.                     }
  136.                     if($this->pageCode < ceil($j / $this->pageSize)){
  137.                         $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ($this->pageCode + 1) . "'>" . $this->siteCode[8] . "</a> ";
  138.                         $this->pageViewText .= "<a href='" . $this->pageUrl . "page=" . ceil($j / $this->pageSize) . "'>" . $this->siteCode[9] . "</a>";
  139.                     }
  140.                     else {
  141.                         $this->pageViewText .= $this->siteCode[8] . " ";
  142.                         $this->pageViewText .= $this->siteCode[9];
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         else {
  148.             $this->showMessage = $this->siteCode[5];
  149.             exit();
  150.         }
  151.         $rs->close();
  152.         $this->conn_close();
  153.     }
  154. }
  155. ?> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值