yaqoob
yaqoob

Reputation: 1062

Fatal Error when creating PHP PDO connection with msaccess using ODBC

For some reasons i need to use ms-access in my PHP application therefore I have enable php_pdo_odbc extension in WAMP. Which shows in phpinfo()

But i am unable to connect the database, it returns the following error.

( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified' in C:\wamp64\www...\portals...\config.php on line 15

( ! )> PDOException: SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified in C:\wamp64\www...\portals...\config.php on line 15

$db_username = ''; //username
$db_password = ''; //password

//path to database file
$database_path = "StudentLogInData.mdb";

//check file exist before we proceed
if (!file_exists($database_path)) {
    die("Access database file not found !");
}

//create a new PDO object
$database = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=realpath($database_path); Uid=$db_username; Pwd=$db_password;");

I'm using WAMP 64bit version on Windows 7 (64-Bit). Searched alot of Stackoverflow's solutions for example tried running 32 bit version but it can't be installed on 64 bit. Please help how to make it work?

Upvotes: 3

Views: 11806

Answers (1)

Álvaro González
Álvaro González

Reputation: 146563

realpath() is a PHP function, not something that the ODBC driver understands. You possibly want to remove it from the DSN:

$database_path = realpath("StudentLogInData.mdb");
$database = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$database_path; Uid=$db_username; Pwd=$db_password;");

As about the driver name, you need to type ODBC in your Start menu and verify the given name is available in your Drivers tab:

ODBC Manager

Upvotes: 3

Related Questions