user1714133
user1714133

Reputation: 79

PHP connecting to access database (.mdb)

I am currently working getting a PHP script to connect to a MS access database 2007, so that the PHP script can retrrive information from it.

Does anyone know any way of accomplishing this I do believe that creating an ODBC Connection may be the way forward but I would appreciate any guidance towards this matter.

Many thanks for any help anyone is willing to give me

James

Upvotes: 2

Views: 19630

Answers (1)

Peter Oram
Peter Oram

Reputation: 6738

Normally, you'd do something like this:

$dbName = $_SERVER["DOCUMENT_ROOT"] . "products\products.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

A successful connection will allow SQL commands to be executed from PHP to read or write the database. If, however, you get the error message “PDOException Could not find driver” then it’s likely that the PDO ODBC driver is not installed. Use the phpinfo() function to check your installation for references to PDO.

If an entry for PDO ODBC is not present, you will need to ensure your installation includes the PDO extension and ODBC drivers. To do so on Windows, uncomment the line extension=php_pdo_odbc.dll in php.ini, restart Apache, and then try to connect to the database again.

In your case I believe it would work if the .mdb file is on an external server, but you'd have to be able to hit it from where you're serving the page.

Not sure if this is intranet or across the public internet, but if it's public internet you might want to rework your work flow a bit, as generally making your database accessible that way is not something you'd want to do.

Source: Using an Access Database with PHP

Upvotes: 3

Related Questions