PHP Cannot connect to PDO ODBC Driver

大城市里の小女人 提交于 2019-12-11 14:44:10

问题


My php cannot find my odbc driver. I've downloaded and re-installed multiple times. Can anyone help me with this error:

QLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data
source name not found and no default driver specified"

Here is my php code:

$dbName = "C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb";
if (!file_exists($dbName))
{
    die("Could not find database file.");
}
try 
{
    // Connect
    $dbh = new PDO("odbc:Driver={Microsoft Access Driver(*.mdb, *.accdb)};Dbq=C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb;Uid=Admin");

    // INSERT data
    $count = $dbh->exec("INSERT INTO part(vendorNo,description,onHand,onOrder,cost,listPrice) VALUES ('$vendorNo', '$desc', '$onHand', '$onOrder', '$cost', '$listPrice')");

    // echo the number of affected rows
    echo $count;

    // close the database connection
    $dbh = null;

}

catch(PDOException $e)
{
    echo $e->getMessage();
} 

I'm running php with apache on xampp. This all on a local machine. My system is 64 bits. I'm not sure if it has something to do with the system and drive types or my syntax or certain drivers I need to install. I just want to insert data from my form into the database on my computer.


回答1:


Driver={Microsoft Access Driver(*.mdb, *.accdb)}

is not a valid ODBC driver name because it is missing a space. The correct name for the newer "ACE" ODBC driver is

Driver={Microsoft Access Driver (*.mdb, *.accdb)}

However, in this case PHP is running in the 32-bit environment and trying to open an .mdb database so the older "Jet" ODBC driver ...

Driver={Microsoft Access Driver (*.mdb)}

... will work, too.




回答2:


Can you place the path after escaping the slashes and then try:-

$dbName = "C:\\Users\\David\\Documents\\SCHOOLNEW\\Assignment5-PROG1800\\database\\as4.mdb";

Escape the slashes in all the paths you have provided in the code and then try.



来源:https://stackoverflow.com/questions/36417549/php-cannot-connect-to-pdo-odbc-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!