Connecting to Access Database with PHP

人盡茶涼 提交于 2020-01-06 04:18:08

问题


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

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