前些天下载了adodb,想用adodb连access数据库,后来连是连上了,不过不能更新和插入记录,也不知道为什么到现在还没人给我回答那个苦恼的问题,后来就放弃了adodb,使用php自己的odbc,但是使用很不方便,就写下了下面这些函数,还没有封装成类,希望能够为有同样问题的朋友一些帮助
<?php
/*
* @ access class
* insert,update,delete record
* version 1.0
* date 2005.6
* power by samsun manzalo (34n 猪八戒)
*/
//=====================================================
// 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;
}
?>
新闻热点
疑难解答