1.使用ODBC 方式连接
<?PHP //创建ADO连接 header("Content-type:text/html;charset=gb2312"); // $dsn = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("22222.mdb"); #access2003 $dsn = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=". realpath("global.asa"); #access2007 $conn = odbc_connect($dsn,"","") or die("error"); $sqlquery = "SELECT * FROM `表名`"; $exec = odbc_exec($conn,$sqlquery); if (!$exec){exit("Error in SQL");} while($row=odbc_fetch_array($exec)) { echo "<br/>value:".$row['字段名']; } odbc_close($conn); ?>
2.使用ADODB方式连接
ps: PHP 5.4.5后,com/dotnet 模块已经成了单独的扩展,所以需要在PHP.ini中配置extension=php_com_dotnet.dll ,如果PHP VERSION<5.4.5 则不需要
<?PHP //创建ADO连接 header("Content-type:text/html;charset=gb2312"); $conn = new COM("ADODB.Connection") or die ("ADO连接失败!"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("global.asa"); $conn->Open($connstr); //创建记录集查询 $rs = @new COM("ADODB.RecordSet"); $rs->Open("select * from productlist",$conn,1,3); while (!$rs->EOF) {//遍历记录集 echo $rs->fields['p_name'] . '<br>'; $rs->MoveNext();//将指针指到下一条记录,否则出现死循环! } $rs->Close();//关闭以便释放内存 ?>