1. 二宝博客首页
  2. php

参考别人写的access函数

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

原创文章,作者:键盘游走者,如若转载,请注明出处:https://www.708034.com/2008/01/%e5%8f%82%e8%80%83%e5%88%ab%e4%ba%ba%e5%86%99%e7%9a%84access%e5%87%bd%e6%95%b0/

发表评论

电子邮件地址不会被公开。 必填项已用*标注