我是个小白,想用php操作mdb文件。(win10,phpstudy8.0,apache2.4.39,php7.3.4)
从网上找了段代码如下:<?php //读取mdb数据库例程 1.连接数据库
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("ATT.MDB");
$conn->open($connstr);
$rs = new com("ADODB.RecordSet");
$rs->Open("select * from USER",$conn,1,1);
//print_r $rs;
while(! $rs->eof) {
$f = $rs->Fields(1);
echo $f->value;
echo $rs->Fields(2)->value;
$rs->MoveNext();
}
?>浏览器报错
Fatal error: in E:\php\phpstudy_pro\WWW\mdb.php on line 4在百度搜了半天没找到有用的信息,哪位大神给点拨一下。

解决方案 »

  1.   

    你可以参考一下我这个类函数里 ACCESS 的链接方法 public function connection(){
    global $log;
    switch($this->db_type){
    case dbt_MS_ACCESS:
    if(server_system==os_windows){
    // 在 Windows 平台用 COM 组件的 ADO 对象
    if(strlen($this->db_accounts)>0){
    $uid = "UID=".$this->db_accounts.';';
    }
    if(strlen($this->db_password)>0){
    $pid = "PWD=".$this->db_password.';';
    }
    try{
    $this->conn->open('Provider=Microsoft.Jet.OLEDB.4.0;'.$uid.$pid.'Data Source='.$this->db_database);
    }catch(Exception $e){
    $log->output('dbclass_','connection ACCESS - error: '.$e->getMessage(),0);
    return;
    }
    }else{
    // 在 Linux 平台使用 ODBC 链接 ACCESS 数据库
    try{
    $this->conn = new PDO('odbc:'.$this->db_database,$this->db_accounts,$this->db_password);
    }catch(Exception $e){
    $log->output('dbclass_','connection ACCESS - error: '.$e->getMessage(),0);
    return;
    }
    try{
    $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }catch(Exception $e){
    $log->output('dbclass_','connection ACCESS - error: '.$e->getMessage(),0);
    return;
    }
    }
    break;
    case dbt_MS_SQL2000:
    case dbt_MS_SQL2005:
    case dbt_MS_SQL2008:
    case dbt_MS_SQL2012:
    if(server_system==os_windows){
    // 在 Windows 平台用 COM 组件的 ADO 对象
    if(strlen($this->db_accounts)>0){
    $uid = "UID=".$this->db_accounts.';';
    }
    if(strlen($this->db_password)>0){
    $pid = "PWD=".$this->db_password.';';
    }
    try{
    $log->output('dbclass_','connection windows SQL2000-SQL2012 : PROVIDER=MSDASQL;DRIVER={SQL SERVER};SERVER='.$this->db_server.';'.$uid.$pid.'DATABASE='.$this->db_database.';',0);
    $this->conn->open('PROVIDER=MSDASQL;DRIVER={SQL SERVER};SERVER='.$this->db_server.';'.$uid.$pid.'DATABASE='.$this->db_database.';');
    }catch(Exception $e){
    $log->output('dbclass_','connection windows SQL2000-SQL2012 - error: '.$e->getMessage(),0);
    return;
    }
    }else{
    // 在 Linux 平台根据版本不同使用不同接口
    switch(intval(php_ver)){
    case 4:
    case 5:
    case 6:
    break;
    case 7:
    try{
    $log->output('dbclass_','connection Linux SQL2000-SQL2012 : sqlsrv:Server='.$this->db_server.';Database='.$this->db_database.';'.$this->db_accounts.';'.$this->db_password,0);
    $this->conn = new PDO('sqlsrv:Server='.$this->db_server.';Database='.$this->db_database.';', $this->db_accounts, $this->db_password);
    }catch(Exception $e){
    $log->output('dbclass_','connection Linux SQL2000-SQL2012 - error: '.$e->getMessage(),0);
    return;
    }
    try{
    $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }catch(Exception $e){
    $log->output('dbclass_','connection SQL2000-SQL2012 - error: '.$e->getMessage(),0);
    return;
    }
    break;
    }
    }
    break;
    case dbt_MYSQL:
    switch(intval(php_ver)){
    case 4:
    case 5:
    case 6:
    try{
    $log->output('dbclass_','connection MYSQL 4/5/6 server :'.$this->db_server.' uid:'.$this->db_accounts.' pwd:'.$this->db_password.' db:'.$this->db_database);
    $this->conn = mysql_connect($this->db_server,$this->db_accounts,$this->db_password);
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 4/5/6 - error: '.$e->getMessage(),0);
    return;
    }
    if(!$this->conn){
    $this->conn= null;
    $log->output('dbclass_','connection MYSQL 4/5/6  - error: '.mysql_error(),0);
    return false;
    }
    try{
    mysql_query("set names utf8"); // 以utf8读取数据
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 4/5/6 - error: '.$e->getMessage(),0);
    return;
    }
    if(strlen($this->db_database)>0){
    try{
    mysql_select_db($this->db_database,$this->conn); // 数据库
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 4/5/6 - error: '.$e->getMessage(),0);
    return;
    }
    }
    break;
    case 7:
    if(strlen($this->db_database)>0){
    if($this->db_port==0){
    try{
    $log->output('dbclass_','connection MYSQL 7 database - mysqli_connect("'.$this->db_server.'","'.$this->db_accounts.'","'.$this->db_password.'","'.$this->db_database.'");');
    $this->conn = mysqli_connect($this->db_server,$this->db_accounts,$this->db_password,$this->db_database);
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 7 database - error: '.$e->getMessage(),0);
    return;
    }
    }else{
    try{
    $log->output('dbclass_','connection MYSQL 7 database - mysqli_connect("'.$this->db_server.'","'.$this->db_accounts.'","'.$this->db_password.'","'.$this->db_database.'",'.$this->db_port.');');
    $this->conn = mysqli_connect($this->db_server,$this->db_accounts,$this->db_password,$this->db_database,$this->db_port);
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 7 database - error: '.$e->getMessage(),0);
    return;
    }
    }
    if (!$this->conn){
    $log->output('dbclass_','connection MYSQL 7 database - error: '.mysqli_connect_error(),0);
    return;
    }
    }else{
    try{
    $log->output('dbclass_','connection MYSQL 7 no database - mysqli_connect("'.$this->db_server.'","'.$this->db_accounts.'","'.$this->db_password.'");');
    $this->conn = mysqli_connect($this->db_server,$this->db_accounts,$this->db_password);
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL 7 no database - error: '.$e->getMessage(),0);
    return;
    }
    if (!$this->conn){
    $log->output('dbclass_','connection MYSQL 7 no database - error: '.mysqli_connect_error(),0);
    return;
    }
    }
    if(mysqli_connect_errno($this->conn)){ 
    $this->conn= null;
    return false;

    try{
    mysqli_query($this->conn,"set names utf8"); // 以utf8读取数据
    }catch(Exception $e){
    $log->output('dbclass_','connection MYSQL - error: '.$e->getMessage(),0);
    return;
    }
    break;
    }
    break;
    } }
      

  2.   

    初始化配置public function init_config($str_type,$str_server,$str_accounts,$str_password,$str_database,$int_port=0){
    global $log;
    $this->db_type = $str_type;
    $this->db_server = $str_server;
    $this->db_accounts = $str_accounts;
    $this->db_password = $str_password;
    $this->db_database = $str_database;
    $this->db_port = $int_port;
    switch($this->db_type){
    case dbt_MS_ACCESS:
    case dbt_MS_SQL2000:
    case dbt_MS_SQL2005:
    case dbt_MS_SQL2008:
    case dbt_MS_SQL2012:
    if(server_system==os_windows){
    // 在 Windows 平台用 COM 组件的 ADO 对象
    try{
    $this->conn = new COM("ADODB.Connection",NULL,65001) or die("ADO connect failed!");
    }catch(Exception $e){
    $log->output('dbclass_','Connection init_config ACCESS/SQL2000-SQL2012 - error: '.$e->getMessage().'(1809170001)',0);
    return;
    }
    }else{
    // 在 Linux 平台
    $this->conn = null;
    }
    break;
    case dbt_MYSQL:
    $this->conn = null;
    break;
    }
    }