程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php+odbc+access數據庫操作函數,在windows下測試通過

php+odbc+access數據庫操作函數,在windows下測試通過

編輯:關於PHP編程

    前些天下載了adodb,想用adodb連access數據庫,後來連是連上了,不過不能更新和插入記錄,也不知道為什麼到現在還沒人給我回答那個苦惱的問題,後來就放棄了adodb,使用php自己的odbc,但是使用很不方便,就寫下了下面這些函數,還沒有封裝成類,希望能夠為有同樣問題的朋友一些幫助

<?php
/*
 * @ access class
 * insert,update,delete record
 * version 1.0
 * date 2005.6
 * power by Samsun Manzalo (34n 豬八戒)
 * www.knowsky.com
 */

//====================================
// insert record
// 插入記錄
//====================================
function insRd($table,$field){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $tmpA =  explode(,,$field);
 $ins = ;
 for($i=0;$i<count($tmpA);$i++){
  $ins.= "".$_POST[$tmpA[$i]].",";
 }
 $ins = substr($ins,0,-1);
 $sql    =  "INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
 //echo $sql;exit;
 $query  = @odbc_do($connid,$sql);
}


//====================================
// get one record detail
// 取得當條記錄詳細信息
//====================================
function getInfo($table,$field,$id,$colnum){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $sql    = "select * from ".$table." where ".$field."=".$id;
 $query  = @odbc_do($connid,$sql);
 
 if(odbc_fetch_row($query)){
  for($i=0;$i<$colnum;$i++){
   $info[$i] = odbc_result($query,$i+1);
  }
 }
 return $info;
}


//====================================
// get record list
// 取得記錄列表
//====================================
function getList($table,$field,$colnum,$condition,$sort="order by id desc"){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $sql = "select * from ".$table." ".$condition." ".$sort;
 $query  = @odbc_do($connid,$sql);
 //echo $sql."<br>";
 $i  = 0;
 while(odbc_fetch_row($query)){
  $rdList[$i] = getInfo($table,$field,odbc_result($query,1),$colnum);
  $i++;
 }
 return $rdList;
}


//====================================
// get record list condition
// 取得記錄列表
//====================================
function getFieldList($table,$field,$fieldnum,$condition="",$sort=""){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $sql = "select ".$field." from ".$table." ".$condition." ".$sort;
 $query  = @odbc_do($connid,$sql);
 //echo $sql."<br>";
 $i  = 0;
 while(odbc_fetch_row($query)){
  for($j=0;$j<$fieldnum;$j++){
   $info[$j] = odbc_result($query,$j+1);
  }
  $rdList[$i] = $info;
  $i++;
 }
 return $rdList;
}

//====================================
// update record
// 更新記錄
//====================================
function updateInfo($table,$field,$id,$set){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $sql = "update ".$table." set ".$set." where ".$field."=".$id;
 $query  = @odbc_do($connid,$sql);
}


//====================================
// record delete
// 刪除記錄
//====================================
function delRd($table,$field,$id){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
    $sql = "delete from ".$table." where ".$field."=".$id;
    $query  = @odbc_do($connid,$sql);
}


//====================================
// record delete cat
// 刪除記錄(條件)
//====================================
function delOrRd($table,$condition){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
    $sql = "delete from ".$table." where ".$condition;
    $query  = @odbc_do($connid,$sql);
}


//====================================
// count record
// 取得記錄數
//====================================
function countRd($table,$condition=""){
 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb";
 $connid  = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("數據庫連接錯誤!");
 $sql     =  "select count(*) as num from ".$table." ".$condition;
 $query   = @odbc_do($connid,$sql);
 odbc_fetch_row($query);
 $num     =  odbc_result($query,1);
 return $num;
}

?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved