Link to home
Start Free TrialLog in
Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PDO Connection string for PHP to MS Access

Hi all,

I am having trouble connecting to my .accdb database.  It's the first time I have tried using Access with PHP.

I have seen that doubling the slashes in the string sometimes works, so I have tried both versions. File_exists() proves positive in both cases, but I get this error:

PHP 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 /home/linweb07/f/xxxxxx.org-78/user/htdocs/NT/index.php:45

Here's my code (phpinfo shows PDO odbc is enabled).

Any assistance much appreciated.

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

$dbName = $_SERVER["DOCUMENT_ROOT"] .DS. "NT" .DS. "ProductData.accdb";
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbName2; Uid=; Pwd=;");

$dbName2 = $_SERVER["DOCUMENT_ROOT"] .DS.DS. "NT" .DS.DS. "ProductData.accdb";
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbName2; Uid=; Pwd=;");
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Why not catch the PDOException and see what else it contains?  This is probably a path error that can be corrected if we can see the data in $_SERVER['DOCUMENT_ROOT'] and the directory structure.

Also, you might want to add error_reporting(E_ALL) to your scripts.  In the first of the examples, the DSN is undefined, but you won't know unless you tell PHP to bring up the Notice messages.

And I would think that redefining the directory separator constant, while it may save typing, probably does not contribute anything to the process.  The slashes are doubled to form an escape sequence in variable assignment.  I don't believe this would not be needed in constructing a data set name.
Avatar of Colin Brazier

ASKER

Hi Ray,

Thanks for getting back so soon.  Er, how do I do that?  I got the info I provided from the error log.

It's also just dawned on me that it's a linux server - would that be the issue?

Cheers,

  Col
It seems that the question now changes to "What is the connection string for a .accdb database on a linux server?" as I found this...

If ODBC fails to find the driver specified, e.g. you use Microsoft Access Driver (*.mdb) in Linux:

'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0
[unixODBC][Driver Manager]Data source name not found, and no default
driver specified'
A Linux Server... Not sure about MS Access on Linux.  Maybe when Dave Baldwin wakes up he will have some insights.  From what I can find with Google, there seem to be a lot of "it depends" situations.

in the mean time, please show us the file_exists() path and the contents of DOCUMENT_ROOT.  There may be a way to get that to match up so that the script can find the DB
This is the file_exists path:  /home/linweb07/f/fobgfc.org-1071537422/user/htdocs/NT/RetailSupplierProductData.accdb

Document root in bold.

It's not critical, I can always port the Access data to MySQL, but my client has an Access DB and I was hoping to prove the concept.

I have also enlisted the help of my host company (Fasthosts).

Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dave, think I'll stop there then.

Cheers both.
You're welcome.  I think that's the right decision.
I agree - that's the right way to go.
You can use Access as a 'front end' to MySQL by installing the MySQL ODBC connector on the Windows machine.  If that helps any.