PHP访问ACCESS数据库的方法总结

在PHP中连接ACCESS有三种方式:
1、创建系统数据源,用php提供的odbc函数即可

2、同样使用php的odbc函数,但不创建数据源。
      $connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath("netBook.mdb") ;
      $connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );

3、使用微软的ADODB数据库驱动
      $conn = new com("ADODB.Connection");
      $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("netBook.mdb"); 
      $conn->Open($connstr);
      $rs = new com("ADODB.RecordSet");
      $rs->Open("select * from class",$conn,1,1);
      while(! $rs->eof) {
            // $f = $rs->Fields(0);
            // echo $f->value;
           echo $rs->Fields["content"]->value."
";
           $rs->MoveNext();
      }
?>

举例如下:

$connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath("1.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );
$query=odbc_do($connid,"select keyword from keyword");
//$query= odbc_do($con,$sql);
$num = 0;
while(odbc_fetch_row($query)){
        //echo $record[$num] = odbc_result($query,1); echo " ";
        //echo $record1[$num] = odbc_result($query,2); echo " ";
        //echo $record2[$num] = odbc_result($query,3); echo " ";
        //echo $record3[$num] = odbc_result($query,4); echo " ";
        //echo "
";
        echo $record = odbc_result($query,1);
        echo "
";
        $num = $num + 1;
        if ($num > 20){  break;  }    // 只取出前20条记录
}
echo $connid;
?>

你可能感兴趣的:(PHP)