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

來源:互聯網
上載者:User
關鍵字 php+odbc+access資料庫操作函數 在windows下測試通過

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

/*
 * @ access class
 * insert,update,delete record
 * version 1.0
 * date 2005.6
 * power by Samsun Manzalo (34n 豬八戒)
 * www.cncms.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 $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."
";
$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."
";
$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;
}

?>

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.