PHP连接ACCESS主要有两种方法:
1.使用PHP的ODBC函数
实例:
<html> <body> <?php ///利用ODBC 读取mdb数据库例程 $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("Grades.mdb"); $conn=odbc_connect($connstr,"root","owenyang"); if(!$conn){ exit("Connection failed:".$conn); echo odbc_errormsg ($conn ); } $sql="select * from grade where id > 4400"; $rs=odbc_exec($conn,$sql); if(!$rs){ exit("error in sql"); } echo "<table><tr>"; echo "<th>Company</th>"; echo "<th>Contact</th>"; while(odbc_fetch_row($rs)){ $company=odbc_result($rs,1); $conname=odbc_result($rs,2); echo "<tr><td>$company</td>"; echo "<td>$conname</td></tr>"; } odbc_close($conn); echo "</table>"; ?> </body> </html>
2.使用微软的ADODB数据库驱动
实例:
<?php //读取mdb数据库例程 $conn=new COM("ADODB.Connection"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .realpath("Grades.mdb")."; Uid=root; Pwd=owenyang"; $conn->Open($connstr); if(!$conn){ exit("Connection failed:".$conn); echo odbc_errormsg ($conn ); } $sql = "select * from grade"; // $rs = new com("ADODB.RecordSet"); // $rs->Open($sql,$conn); $rs=$conn->Execute($sql); //该行可与以上两行互换 while(! $rs->eof) { $f = $rs->Fields(1); echo $f->value; echo "<td> ". $rs->Fields(2)->value; echo "</td><br>"; $rs->MoveNext(); } $conn->close(); //$rs->close(); ?>
一般情况下,我们都是PHP+MYSQL。但今天心血来潮,想做一下PHP+ACCESS。网上搜了一些资料,记录下来,以备后查。