douquanzhan0315 2014-05-29 10:58
浏览 119
已采纳

错误:找不到驱动程序 - 将PDO与MS Access数据库一起使用

Background:

I've got a fully working Microsoft Access DB. I've made a database connection class and just a simple page that includes the class and fires off a simple SQL code. I know the code is right as it was working fine a few weeks ago. However, in between then and now I installed PHP, MySQL, set up my IIS and installed PHPMyAdmin. (We were having problems with our servers so tried going localhost but it was resolved before I fully used PHPMyAdmin).

So now I've got my connection class and simple php page onto the server (using FTP). However, when I run the same query I used a few weeks ago i'm now getting the error message:

ERROR:could not find driver. Warning: file_put_contents(connection.errors.txt) [function.file-put-contents]: failed to open stream: Permission denied in E:\kunden\blah\blah\blah\www\simpleTest.php on line 31

The Code

connectionClass.php:

class connection{

public $con;
private $dbName;

function __construct(){
$this->dbName = $_SERVER["DOCUMENT_ROOT"] . "../database/db.mdb";
 }

function connect(){
    $this->con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$this->dbName; Uid=Admin; Pwd=;");
    $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    return $this->con;
 }   
}

simpleTest.php

if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}

try{
include_once 'classes/connectionClass.php';

//get the DB connection
$con = new connection();
$pdoConnection = $con->connect();

//query the DB
$sql = $pdoConnection->prepare("SELECT * FROM celebs");
$result = $sql->execute();
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {        
    echo $row['firstname'];
    echo $row['surname'];
 }

} catch (Exception $e){
echo 'ERROR:'.$e->getMessage();
file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);
}

I've not changed the code and was wondering if setting up PHP, MySQL, IIS and PHPMyAdmin has done something to prevent me from my code working? I've looked in phpinfo(); but i'm not really sure what to be looking for.

Any help is MUCH appreciated, thank you.

EDIT: In addition, after some de-bugging - i'm fairly certain the error revolves around the code in simpleTest.php after trying to make a new connection...

  • 写回答

1条回答 默认 最新

  • duanhuan1147 2014-05-29 13:13
    关注

    Manipulating an Access database from PHP via ODBC has some serious limitations that affect both PDO and the older odbc_exec methods. If you are using a Windows server and you absolutely must use an Access database back-end (which is strongly discouraged) I would recommend that you use ADO under com_dotnet like this:

    <?php
    // this code requires the following php.ini directive:
    //
    // extension=php_com_dotnet.dll
    
    $con = new COM("ADODB.Connection"); 
    $con->Open(
            "Provider=Microsoft.Jet.OLEDB.4.0;" .
            "Data Source=C:\\Users\\Public\\mdbTest.mdb");
    $rst = new COM("ADODB.Recordset");
    $rst->Open("SELECT * FROM celebs", $con, 1, 3);  // adOpenKeyset, adLockOptimistic
    while (!$rst->EOF) {
        echo $rst["firstname"]->Value . " " . $rst["surname"]->Value . "<br/>";
        $rst->MoveNext;
    }
    $rst->Close();
    $con->Close();
    

    This is especially true if you ever need full Unicode character support or expect to be manipulating binary objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法