Colin Brazier
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.or g-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={Microsof t Access Driver (*.mdb, *.accdb)};Dbq=$dbName2; Uid=; Pwd=;");
$dbName2 = $_SERVER["DOCUMENT_ROOT"] .DS.DS. "NT" .DS.DS. "ProductData.accdb";
$db = new PDO("odbc:Driver={Microsof t Access Driver (*.mdb, *.accdb)};Dbq=$dbName2; Uid=; Pwd=;");
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.or
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={Microsof
$dbName2 = $_SERVER["DOCUMENT_ROOT"] .DS.DS. "NT" .DS.DS. "ProductData.accdb";
$db = new PDO("odbc:Driver={Microsof
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
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
ASKER
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'
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
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
ASKER
This is the file_exists path: /home/linweb07/f/fobgfc.or g-10715374 22/user/ht docs/NT/RetailSupplierProductD ata.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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dave, think I'll stop there then.
Cheers both.
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.
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.