原生PHP连接access数据库及操作

1、数据写入方法:

$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" . realpath("hydb/#db#.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );					//连接数据源
$sql="insert into openid (nam,gsid) values('$fromUsername',’$gsid’)"; 
$result=odbc_exec($connid,$sql);										//执行语句
if($result) echo "successful";
else echo "failed";
odbc_close($connid);  													//关闭 ODBC 连接

2、数据读取方法:

$conn=new COM("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" . realpath("hydb/#db#.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );					//连接数据源
$sql ="select * from config";  
$query = odbc_exec($connid, $sql); 
if($query){
  while (odbc_fetch_row($query)){
    echo odbc_result($query,"wxid")."
"; //循环输出字段为wxid的数据 echo odbc_result($query,"gsid")."
"; } }else{ print "No data"; } odbc_close($connid);

3、删除数据方法:

$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
$conn->Open($connstr);  
$query   =   "delete   from   userinfo   where   uid=1";  
$result   =$conn->Execute($query);  
if($result)  
echo   "删除成功";  
else
echo   "删除失败";  

4、更新数据方法:

$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../mydata.mdb");
$conn->Open($connstr);  
$query   =   "update userinfo set username='ganjun'   where   uid=2";  
$result   =$conn->Execute($query);  
if($result)  
echo   "更新数据成功";  
else
echo   "更新数据失败";  

5、PHP Warning: odbc_connect(): SQL error: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序, SQL state IM002 in SQLConnect in E:\wwwroot\phperz.com\phpweb\conn.php on line 8问题的解决办法:
检查服务器控制面板->管理工具->数据源(ODBC)->系统DSN(是否有Microsoft Access Driver (*.mdb, .accdb)或Microsoft Access Driver (.mdb),若没有则下载并安装AccessDatabaseEngine_X64.exe驱动,注意服务器是32还是64位的)->填写数据源名、选择对应的数据库.mdb文件 ->2次确定。


你可能感兴趣的:(php)