ACCESS PHP操作类

本文介绍了一个用于操作Access数据库的PHP类,包括连接数据库、执行查询、获取记录等核心功能。

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

  1. <?php
  2. --------------------------------------------------------------------
  3. //FileName:class.php
  4. //Summary: Access数据库操作类
  5. //Author: forest
  6. //CreateTime: 2006-8-10
  7. //LastModifed:
  8. //copyright (c)2006
  9. //http://freeweb.nyist.net/~chairy
  10. //[email]chaizuxue@163.com[/email]
  11. // 使用范例:
  12. //$databasepath="database.mdb";
  13. //$dbusername="";
  14. //$dbpassword="";
  15. //include_once("class.php");
  16. //$access=new Access($databasepath,$dbusername,$dbpassword);
  17. --------------------------------------------------------------------
  18. class Access
  19. {
  20.     var $databasepath,$constr,$dbusername,$dbpassword,$link;
  21.     function Access($databasepath,$dbusername,$dbpassword)
  22.         {
  23.             $this->databasepath=$databasepath;
  24.             $this->username=$dbusername;
  25.             $this->password=$dbpassword;
  26.             $this->connect();
  27.         }
  28.     function connect()
  29.         {
  30.             $this->constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->databasepath);
  31.             $this->link=odbc_connect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);
  32.             return $this->link;
  33.             //if($this->link) echo "恭喜你,数据库连接成功!";
  34.             //else echo "数据库连接失败!";
  35.         }
  36.     function query($sql)
  37.         {
  38.             return @odbc_exec($this->link,$sql);
  39.         }
  40.     function first_array($sql)
  41.         {
  42.             return odbc_fetch_array($this->query($sql));
  43.         }
  44.     function fetch_row($query)
  45.         {
  46.             return odbc_fetch_row($query);
  47.         }
  48.     function total_num($sql)//取得记录总数
  49.         {
  50.             return odbc_num_rows($this->query($sql));
  51.         }
  52.     function close()//关闭数据库连接函数
  53.         {
  54.             odbc_close($this->link);
  55.         }
  56.     function insert($table,$field)//插入记录函数
  57.         {
  58.             $temp=explode(',',$field);
  59.             $ins='';
  60.             for ($i=0;$i<count($temp);$i++)
  61.             {
  62.             $ins.="'".$_POST[$temp[$i]]."',";
  63.             }
  64.             $ins=substr($ins,0,-1);
  65.             $sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
  66.             $this->query($sql);
  67.         }
  68.     function getinfo($table,$field,$id,$colnum)//取得当条记录详细信息
  69.         {
  70.             $sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
  71.             $query=$this->query($sql);
  72.             if($this->fetch_row($query))
  73.             {
  74.                 for ($i=1;$i<$colnum;$i++)
  75.                 {
  76.                     $info[$i]=odbc_result($query,$i);
  77.                 }
  78.             }
  79.             return $info;
  80.         }
  81.     function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得记录列表
  82.     {
  83.         $sql="SELECT * FROM ".$table." ".$condition." ".$sort;
  84.         $query=$this->query($sql);
  85.         $i=0;
  86.         while ($this->fetch_row($query))
  87.         {
  88.         $recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
  89.         $i++;
  90.         }
  91.         return $recordlist;
  92.     }
  93.     function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得记录列表
  94.     {
  95.             $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
  96.             $query=$this->query($sql);
  97.             $i=0;
  98.             while ($this->fetch_row($query))
  99.             {
  100.             for ($j=0;$j<$fieldnum;$j++)
  101.             {
  102.             $info[$j]=odbc_result($query,$j+1);
  103.             }
  104.             $rdlist[$i]=$info;
  105.             $i++;
  106.             }
  107.             return $rdlist;
  108.     }
  109.     function updateinfo($table,$field,$id,$set)//更新记录
  110.     {
  111.         $sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
  112.         $this->query($sql);
  113.     }
  114.     function deleteinfo($table,$field,$id)//删除记录
  115.     {
  116.         $sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
  117.         $this->query($sql);
  118.     }
  119.     function deleterecord($table,$condition)//删除指定条件的记录
  120.     {
  121.         $sql="DELETE FROM ".$table." WHERE ".$condition;
  122.         $this->query($sql);
  123.     }
  124.     function getcondrecord($table,$condition="")// 取得指定条件的记录数
  125.     {
  126.         $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
  127.         $query=$this->query($sql);
  128.         $this->fetch_row($query);
  129.         $num=odbc_result($query,1);
  130.         return $num;
  131.     }
  132. }
  133. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值