SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Ted G used Ask the Experts™
on
I'm using PHP to connect to an Acccess database. Any way I write my connection string, I get SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.

I see someone else figured it was a 64 bit vs 32 bit issues, I'm wondering if I have the same. I've created my DSN and even when I don't use the DSN and give the file path I get this error. I'm on a windows 10 machine and downloaded PHP today. Could this be that the PHP not compatible with 32 bit ODBC? Thanks for any help you can provide.

<?php

$dbName =$_SERVER["DOCUMENT_ROOT"] ."\PhillyCanineSportsClub\Database2.mdb";
//$dbName = 'pcsc'; try {$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=' . $dbName;$dbh = new PDO($connStr); } catch (PDOException$e) {
echo $e->getMessage(); echo$db;

}
?>
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fractional CTO
Distinguished Expert 2018

Commented:
Trying to get Dbq=... to work inside a Webserver environment.

Best to use an absolute path, starting with a drive letter.

The path you have will likely be treated as a path relative to the current working directory at the time... which will likely be pinned to somewhere in your Webserver runtime directory... so will never point to your database...

Test using an absolute path + see if that clears the error you're seeing.

Commented:
Thanks, but it doesn't make a difference when I write
$dbName = "C:\inetpub\wwwroot\PhillyCanineSportsClub\Database2.mdb"; Commented: If I write echo 8 * PHP_INT_SIZE;, I get 64. Should that be 32? Someone else checked this value and it was 32, and someone said 32 was correct. Do I have the wrong version of PHP? Fractional CTO Distinguished Expert 2018 Commented: Run your php file on the command line (which absolute path) + provide the exact error (cut + paste) in a comment. Also post the output of something like... dir C:\inetpub\wwwroot\PhillyCanineSportsClub\Database2.mdb"  Or whatever equivalent command line file lister you use. Fractional CTO Distinguished Expert 2018 Commented: https://stackoverflow.com/questions/27865340/php-int-size-returns-4-but-my-operating-system-is-64-bit provides details. And yes, looks like somehow you've (maybe) installed a 32 bit version of PHP. You'll have to do some deep research to see if this will actually work. Likely best to install a 64 bit version. Commented: not sure how to run the php file from the command line, doesn't seem to work. And do you mean I downloaded a 64 bit instead of 32? My PHP_INT_Size is 8. 8x8=64 Fractional CTO Distinguished Expert 2018 Commented: 1) not sure how to run the php file from the command line, doesn't seem to work. php -f path-to-your-php-file which will look something like this... # php -f /david-favor/wptools/hello-world.php Sun Feb 10 07:53:30 2019 (585735): Message Cached script '/david-favor/wptools/hello-world.php' <html> <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body> </html> 2) And do you mean I downloaded a 64 bit instead of 32? My PHP_INT_Size is 8. 8x8=64 Yes. Most Valuable Expert 2017 Distinguished Expert 2018 Commented: Your code works perfectly here but you need to use double \\ to escape the backslashes. If you change to $dbName = "C:\\inetpub\\wwwroot\\PhillyCanineSportsClub\\Database2.mdb";


Commented:
Thanks Julian, but that didn't make a difference either. Here's the first few lines of the output from running the php file from the command prompt, as well as the dir for the db. I moved everything to C: so I didn't have to worry about slashes, but still same thing.

It is 64 bit PHP, is that not compatible with 32 bit Access? My OS is 64 bit. Is my Access db wrong?

C:\>php -f phpinfo.php
PCSC64<p>Hello World</p>SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specifiedphpinfo()
PHP Version => 7.2.7

System => Windows NT PCSSC 10.0 build 17134 (Windows 10) AMD64
Build Date => Jun 19 2018 23:09:10
Compiler => MSVC15 (Visual C++ 2017)
Architecture => x64
Configure Command => cscript /nologo configure.js  "--enable-snapshot-build" "--enable-debug-pack" "--disable-zts" "--with-pdo-oci=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--without-analyzer" "--with-pgo"
Server API => Command Line Interface
Virtual Directory Support => disabled

C:\>dir Database2.mdb
Volume in drive C is Windows
Volume Serial Number is E65C-7BF6

Directory of C:\

02/09/2019  02:38 PM           221,184 Database2.mdb
1 File(s)        221,184 bytes
0 Dir(s)  107,860,852,736 bytes free

Commented:
I found this...I'm guessing I have to run 32 bit PHP to be compatible with Access, even though my OS is 64.

Kathy S - Microsoft Support
Replied on January 22, 2010
Moderator
Hi smantha11,

Currently Microsoft does not have any 64 bit ODBC drivers available for the Office products.
However, as long as the application that you want to connect to is 32 bit, you can use the 32 bit ODBC drivers to create the DSN.
Fractional CTO
Distinguished Expert 2018

Commented:
You have 2x items to solve.

1) You database path, based on dir above should be...

\$dbName = "C:\\Database2.mdb";


2) The above phpinfo.php error suggests no database driver is being referenced in your PHP config file.

Normally you'll have a directory like /etc/php/7.2/mods-available (or something similar) which should contain many .so objects or in the case of Windows, maybe .dll objects.

Then in /etc/php/7.2/fpm/php-fpm.conf you should see an include of all modules from mods-available, likely through symlinks.

Trying to debug this on a Linux install takes a few seconds, because inotifywait exists to track exactly what files are read during PHP restarts.

On Windows, there's nothing like this.

I'd suggest you use the https://github.com/thekid/inotify-win tool which approximates inotifywait, so you can determine order in which all your PHP config files are being loaded.

Likely something simple to fix... and finding the simple fix... may take some time.

Commented:

I also wonder why the odbc administrator says 64 bit at the top, but at the bottom says it's 32 bit???

Commented:
Changing the database path doesn't make any difference. And in the php.ini file it says this. The last item is the odbc, I added that yesterday. Someone else had suggested that.

[ExtensionList]
extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_curl.dll
extension=php_exif.dll
extension=php_xmlrpc.dll
extension=php_openssl.dll
extension=php_soap.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_imap.dll
extension=php_tidy.dll
extension=php_pdo_odbc.dll
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I connect to access all the time from PHP - no problems.

Only difference is I created an ODBC DSN for it and connect with that rather than directly.

I am assuming you have tried this.

dsn => 32 bit Driver do Microsoft Access (*.mdb)

Commented:
Yes, I have. Do you run 32 or 64 bit PHP? Thanks.
Commented:
I removed the version of php I was using by editing the hostconfig file and then downloaded an x86 version. I still had to edit the php.ini file adding the odbc...extension=php_pdo_odbc.dll at the very bottom of the file, and then it worked. Thanks for the efforts and hope I can help someone else with this issue. Also, I tried installing without using the MS Web Platform Installer and got other errors, so I recommend using the Web Platform Installer.

Do more with