Connect to Access with PHP - php

I need to connect to an Access Database through PHP.
I've uncommented "extension=php_pdo_odbc.dll" in the php.ini file.
I've put the database in the same folder as the .php file I'm trying to connect with.
My code so far:
$dbName = "data.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName");
My error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] SQLDriverConnect: -1811 [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.' in C:\xampp\htdocs\Test\index.php:40 Stack trace: #0
C:\xampp\htdocs\Access_Test\index.php(40): PDO->__construct('odbc:DRIVER={Mi...') #1 {main} thrown in C:\xampp\htdocs\LAWsites\Access_Test\index.php on line 40
Any ideas?

The Database Location parameter (DBQ) requires an absolute path to your database file:
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath($dbName));

I was getting a similar error when the access database was located in the same folder.
SQLSTATE[HY000] SQLDriverConnect: -1044 [Microsoft][ODBC Microsoft Access Driver] Not a valid file name.
I put "./" in front of the filename and it worked.
$handler = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=./CIDB.MDB;");

Related

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]

I am trying to open an access file through php. The code i am using is this:
<?php
$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};'
.'Dbq=C:\\file.accdb;';
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare($sql);
$params = array();
$sth->execute($params);
?>
If i place the file in my IIS C drive, then php file opens just fine. If i try to place the file inside a network location (i have permissions in that network location) then i get the following error:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] SQLDriverConnect: 63 [Microsoft][ODBC Microsoft Access
Driver]General error Unable to open registry key Temporary (volatile) Ace DSN
for process 0x16e0 Thread 0xd84 DBC 0x215b904
Jet'.' in C:\inetpub\wwwroot\app\update.php:56
Stack trace:
#0 C:\inetpub\wwwroot\teldir\update.php(56): PDO->__construct('odbc:Driver={Mi...')
#1 {main} thrown in C:\inetpub\wwwroot\app\update.php on line 56
In ODBC i have created a DSN and selected the file through network location.
Any ideas?
Thank you in advance

'Microsoft Access Driver (*.mdb)' : file not found' ERROR in connecting pdo with odbc in php

I am trying to connect the Microsoft access database using PDO with odbc. I have mounted the files on network drive and I am trying to access them, but I am getting the following error:
PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open lib 'Microsoft Access Driver (*.mdb)'': file not found'.
Here is my code:
$dbName = "/info/new.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$database = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq= $dbName;Uid=admin");
First, make sure the odbc extension is activated in the php.ini file. Just remove the ; to enable it.
;extension=php_pdo_odbc.dll
Then for connection, please use the exact file location with proper convention (it should be using backslash). The example is below.
try {
$file_location = "C:\Users\PC1\Desktop\your_database.mdb";
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_location;Uid=Admin");
//Do your program stuffs here
$dbh = null;
} catch (PDOException $e){
echo $e->getMessage();
}

Getting error in php ms access database connection

I want to go to build an application with PHP and with existing ms access. My xampp server running in local, but unfortunately my MS Access database located in other file server, previously it was running perfectly but suddenly it gives following error MSG
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] SQLDriverConnect: -1032 [Microsoft][ODBC Microsoft Access Driver]
The Microsoft Jet database engine cannot open the file '(unknown)'. It is already opened exclusively by another user,
or you need permission to view its data.' in C:\xampp\htdocs\tbc\test.php:6 Stack trace: #0
C:\xampp\htdocs\tbc\test.php(6): PDO->__construct('odbc:DRIVER={Mi...') #1 {main} thrown in C:\xampp\htdocs\tbc\test.php on line 6
My actual code is:
$dbNameAccess ="//Fileserver/work/access/db.mdb";
if (!file_exists($dbNameAccess)) {
// die("Could not find database file.");
}
$db_na = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbNameAccess; Uid=; Pwd=;");

PHP ODBC - connect to local .mdb database

I've installed the php5-odbc library.
Trying to connect to a .mdb file but keep getting error Data source name not found, and no default driver specified.
Code:
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
Outputs:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in [...]
PDO Drivers:
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
Output:
mysql
odbc
sqlite
The problem is recurrent with 64-bit version. It looks like your code only work on Windows 32-bit.
To solve the problem, you might install this tool: Microsoft Access Database Engine 2010 Redistributable. I had the same problem for months but this tool solved mine.
I guess you're doing the location wrong?
Instead of this
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
Why not do it like this
$dbName = "..\..\..\var\Import\PartsPlaceDB.mdb";

Accessing MDB file with PHP on CentOS machine

I am trying to access a MDB file using PHP on a CentOS machine, preferably using PDO. I have installed the ODBC and pdo_odbc modules in PHP and restarted the the web server (Apache). I also installed mdbtools and unixODBC.
print_r(PDO::getAvailableDrivers());
Shows:
Array
(
[0] => mysql
[1] => odbc
[2] => pgsql
[3] => sqlite
)
I try to access the DBA file (which is saved locally) with this code:
try
{
$driver = "/usr/lib64/libmdbodbc.so.0";
$dbName = "/PATH/FILE.MDB";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER=$driver; DBQ=$dbName; Uid=; Pwd=;");
$sql = "SHOW TABLES";
$result = $db->query($sql);
$row = $result->fetch();
var_dump($row);
}
catch(Exception $e)
{
echo "\n\nEXCEPTION: $e\n\n";
}
And this outputs:
EXCEPTION: exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /PATH/odbctest.php:14
Stack trace:
#0 /PATH/odbctest.php(14): PDO->__construct('odbc:DRIVER=/us...')
#1 {main}
What do I need to change? What should I use as a driver name? I have been looking all over the place and all I find is a lot of really old stuff from the 2004 era, but a lot of the links have disappeared over the years. This is the most relevant StackOverflow question, but does not include any code: PDO Microsoft Access. I really only need to read from the file.
EDIT:
With some tinkering it seems that I now at least have PDO happy with the driver by correcting the odbcinst.ini file which now contains the following:
[MDBTools]
Description = MDBTools Driver
Driver64 = /usr/lib64/libmdbodbc.so.0
Setup = /usr/lib64/libmdbodbc.so.0
FileUsage = 1
UsageCount = 1
Unfortunately, I am still getting an error:
exception 'PDOException' with message 'SQLSTATE[08001] SQLDriverConnect: 1 [unixODBC]Could not find DSN in connect string' in /PATH/odbctest.php:15
Stack trace:
#0 /PATH/odbctest.php(15): PDO->__construct('odbc:Driver=MDB...')
#1 {main}
I am making the PDO call as follows:
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver; DBQ=$dbName; Uid=; Pwd=;");
I tried adding the data source to the odbc.ini file even though I can't do this in the production version and am still getting the same error. I did find this which is interesting: DSN-less connection with PHP ODBC using MDBTools Driver. Any suggestions?
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver;DBQ=$dbName", "", "");

Categories