|
前些天下载了adodb,想用adodb连access数据库,后来连是连上了,不过不能更新和插入记录,也不知道为什么到现在还没人给我回答那个苦恼的问题,后来就放弃了adodb,使用php自己的odbc,但是使用很不方便,就写下了下面这些函数,还没有封装成类,希望能够为有同样问题的朋友一些帮助 Z2WLinux联盟 Z2WLinux联盟 <?php Z2WLinux联盟 /* Z2WLinux联盟 * @ access class Z2WLinux联盟 * insert,update,delete record Z2WLinux联盟 * version 1.0 Z2WLinux联盟 * date 2005.6 Z2WLinux联盟 * power by Samsun Manzalo (34n 猪八戒) Z2WLinux联盟 * Z2WLinux联盟 */ Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // insert record Z2WLinux联盟 // 插入记录 Z2WLinux联盟 //==================================== Z2WLinux联盟 function insRd($table,$field){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $tmpA = explode(',',$field); Z2WLinux联盟 $ins = ''; Z2WLinux联盟 for($i=0;$i<count($tmpA);$i++){ Z2WLinux联盟 $ins.= "'".$_POST[$tmpA[$i]]."',"; Z2WLinux联盟 } Z2WLinux联盟 $ins = substr($ins,0,-1); Z2WLinux联盟 $sql = "INSERT INTO ".$table." (".$field.") VALUES (".$ins.")"; Z2WLinux联盟 //echo $sql;exit; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // get one record detail Z2WLinux联盟 // 取得当条记录详细信息 Z2WLinux联盟 //==================================== Z2WLinux联盟 function getInfo($table,$field,$id,$colnum){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "select * from ".$table." where ".$field."=".$id; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 Z2WLinux联盟 if(odbc_fetch_row($query)){ Z2WLinux联盟 for($i=0;$i<$colnum;$i++){ Z2WLinux联盟 $info[$i] = odbc_result($query,$i+1); Z2WLinux联盟 } Z2WLinux联盟 } Z2WLinux联盟 return $info; Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // get record list Z2WLinux联盟 // 取得记录列表 Z2WLinux联盟 //==================================== Z2WLinux联盟 function getList($table,$field,$colnum,$condition,$sort="order by id desc"){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "select * from ".$table." ".$condition." ".$sort; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 //echo $sql."<br>"; Z2WLinux联盟 $i = 0; Z2WLinux联盟 while(odbc_fetch_row($query)){ Z2WLinux联盟 $rdList[$i] = getInfo($table,$field,odbc_result($query,1),$colnum); Z2WLinux联盟 $i++; Z2WLinux联盟 } Z2WLinux联盟 return $rdList; Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // get record list condition Z2WLinux联盟 // 取得记录列表 Z2WLinux联盟 //==================================== Z2WLinux联盟 function getFieldList($table,$field,$fieldnum,$condition="",$sort=""){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "select ".$field." from ".$table." ".$condition." ".$sort; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 //echo $sql."<br>"; Z2WLinux联盟 $i = 0; Z2WLinux联盟 while(odbc_fetch_row($query)){ Z2WLinux联盟 for($j=0;$j<$fieldnum;$j++){ Z2WLinux联盟 $info[$j] = odbc_result($query,$j+1); Z2WLinux联盟 } Z2WLinux联盟 $rdList[$i] = $info; Z2WLinux联盟 $i++; Z2WLinux联盟 } Z2WLinux联盟 return $rdList; Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // update record Z2WLinux联盟 // 更新记录 Z2WLinux联盟 //==================================== Z2WLinux联盟 function updateInfo($table,$field,$id,$set){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "update ".$table." set ".$set." where ".$field."=".$id; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // record delete Z2WLinux联盟 // 删除记录 Z2WLinux联盟 //==================================== Z2WLinux联盟 function delRd($table,$field,$id){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "delete from ".$table." where ".$field."=".$id; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // record delete cat Z2WLinux联盟 // 删除记录(条件) Z2WLinux联盟 //==================================== Z2WLinux联盟 function delOrRd($table,$condition){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "delete from ".$table." where ".$condition; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 Z2WLinux联盟 //==================================== Z2WLinux联盟 // count record Z2WLinux联盟 // 取得记录数 Z2WLinux联盟 //==================================== Z2WLinux联盟 function countRd($table,$condition=""){ Z2WLinux联盟 $connstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=database/email.mdb"; Z2WLinux联盟 $connid = @odbc_connect($connstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!"); Z2WLinux联盟 $sql = "select count(*) as num from ".$table." ".$condition; Z2WLinux联盟 $query = @odbc_do($connid,$sql); Z2WLinux联盟 odbc_fetch_row($query); Z2WLinux联盟 $num = odbc_result($query,1); Z2WLinux联盟 return $num; Z2WLinux联盟 } Z2WLinux联盟 Z2WLinux联盟 ?> Z2WLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|