问题
I'm trying to connect to my Access database via PHP, but i am getting the following error :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified' in C:\inetpub\wwwroot\agency\getProperty.php:7 Stack trace: #0 C:\inetpub\wwwroot\agency\getProperty.php(7): PDO->__construct('odbc:Driver={Mi...') #1 {main} thrown in C:\inetpub\wwwroot\agency\getProperty.php on line 7
This is the code i am using :
<?php
$bits = 8 * PHP_INT_SIZE;
echo "(Info: This script is running as $bits-bit.)\r\n\r\n";
$dbName = "HS_BE.accdb";
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=HS_BE.accdb");
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM Accommodation";
$sth = $dbh->prepare($sql);
$sth->execute();
while ($row = $sth->fetch()) {
echo $row['Options'] . "\r\n";
}
The HS_BE.accdb is in the same directory as the PHP file, ive checked the PHP.ini and all the odbc lines are un-commented - also, ive downloaded the driver from microsoft.com on the server. The only thing i can think of is my $bits is echoing 32 bit, and the driver i downloaded was 64 bit but i cant download the 32bit driver due to me having 64bit office on the server.
回答1:
To be on the safe side, try using realpath("HS_BE.accdb")
来源:https://stackoverflow.com/questions/36696034/connecting-to-access-database-with-php