参考别人写的access函数

PHP代码
  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. --------------------------------------------------------------------   
  19.     class Access   
  20.     {   
  21.          var $databasepath,$constr,$dbusername,$dbpassword,$link;   
  22.          function Access($databasepath,$dbusername,$dbpassword)   
  23.          {   
  24.                $this->databasepath=$databasepath;   
  25.         $this->username=$dbusername;   
  26.         $this->password=$dbpassword;   
  27.         $this->connect();   
  28.           }   
  29.            
  30.     function connect()   
  31.     {   
  32.         $this->constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->databasepath);    
  33.         $this->link=odbc_connect($this->constr,$this->username,$this->password,SQL_CUR_USE_ODBC);   
  34.         return $this->link;   
  35.         //if($this->link) echo "恭喜你,数据库连接成功!";   
  36.         //else echo "数据库连接失败!";   
  37.     }   
  38.            
  39.     function query($sql)   
  40.     {   
  41.         return @odbc_exec($this->link,$sql);   
  42.     }   
  43.            
  44.     function first_array($sql)   
  45.     {   
  46.         return odbc_fetch_array($this->query($sql));   
  47.     }   
  48.            
  49.     function fetch_row($query)   
  50.     {   
  51.         return odbc_fetch_row($query);   
  52.     }   
  53.            
  54.     function total_num($sql)//取得记录总数   
  55.     {   
  56.         return odbc_num_rows($this->query($sql));   
  57.     }   
  58.            
  59.     function close()//关闭数据库连接函数   
  60.     {       
  61.         odbc_close($this->link);   
  62.     }   
  63.                
  64.     function insert($table,$field)//插入记录函数   
  65.     {   
  66.         $temp=explode(',',$field);   
  67.         $ins='';   
  68.         for ($i=0;$i<count($temp);$i++)   
  69.         {   
  70.             $ins.="'".$_POST[$temp[$i]]."',";   
  71.         }   
  72.         $ins=substr($ins,0,-1);   
  73.         $sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";   
  74.         $this->query($sql);   
  75.     }   
  76.            
  77.     function getinfo($table,$field,$id,$colnum)//取得当条记录详细信息   
  78.     {   
  79.         $sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";   
  80.         $query=$this->query($sql);   
  81.         if($this->fetch_row($query))   
  82.         {   
  83.             for ($i=1;$i<$colnum;$i++)   
  84.             {   
  85.           $info[$i]=odbc_result($query,$i);   
  86.              }   
  87.          }   
  88.          return $info;   
  89.     }   
  90.            
  91.     function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得记录列表       
  92.     {   
  93.          $sql="SELECT * FROM ".$table." ".$condition." ".$sort;   
  94.          $query=$this->query($sql);   
  95.          $i=0;   
  96.          while ($this->fetch_row($query))    
  97.          {   
  98.         $recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);   
  99.         $i++;   
  100.           }   
  101.           return $recordlist;   
  102.     }   
  103.            
  104.     function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得记录列表   
  105.     {   
  106.          $sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;   
  107.          $query=$this->query($sql);   
  108.          $i=0;   
  109.          while ($this->fetch_row($query))    
  110.          {   
  111.          for ($j=0;$j<$fieldnum;$j++)   
  112.         {   
  113.                    $info[$j]=odbc_result($query,$j+1);   
  114.         }       
  115.         $rdlist[$i]=$info;   
  116.         $i++;   
  117.          }   
  118.          return $rdlist;   
  119.     }   
  120.            
  121.     function updateinfo($table,$field,$id,$set)//更新记录   
  122.     {   
  123.         $sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;   
  124.         $this->query($sql);   
  125.     }   
  126.            
  127.     function deleteinfo($table,$field,$id)//删除记录   
  128.     {   
  129.          $sql="DELETE FROM ".$table." WHERE ".$field."=".$id;   
  130.          $this->query($sql);   
  131.     }   
  132.            
  133.     function deleterecord($table,$condition)//删除指定条件的记录   
  134.     {   
  135.          $sql="DELETE FROM ".$table." WHERE ".$condition;   
  136.          $this->query($sql);   
  137.     }   
  138.            
  139.     function getcondrecord($table,$condition="")// 取得指定条件的记录数   
  140.     {   
  141.          $sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;   
  142.          $query=$this->query($sql);   
  143.          $this->fetch_row($query);   
  144.          $num=odbc_result($query,1);   
  145.          return $num;               
  146.     }   
  147.      }   
  148. ?>   
posted @ 2008-01-06 03:14  二宝的博客  阅读(146)  评论(0编辑  收藏  举报