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=;");
Colin BrazierAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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.
Colin BrazierAuthor Commented:
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?


Colin BrazierAuthor Commented:
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'
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ray PaseurCommented:
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
Colin BrazierAuthor Commented:
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.
Dave BaldwinFixer of ProblemsCommented:
I don't think you will be able to make it work from Linux.  All the Access drivers are Windows only.  And Access is file based and not server based so you can't call the server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Colin BrazierAuthor Commented:
Thanks Dave, think I'll stop there then.

Cheers both.
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  I think that's the right decision.
Ray PaseurCommented:
I agree - that's the right way to go.
Dave BaldwinFixer of ProblemsCommented:
You can use Access as a 'front end' to MySQL by installing the MySQL ODBC connector on the Windows machine.  If that helps any.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.