Microsoft Access using PHP PDO

Microsoft Access database is not a preferred choice to store sensitive or large amount of data, because it is not so robust as other database, you will soon discover several limitation as your database size grows. However for small business and individuals Access database is ideal alternative, it is cheap to setup and simple to use! Let’s find out how easily we can play with Access database using PHP PDO.

Let’s create a database file called “database.mdb” using Microsoft Access, and create a table called “users” as shown in picture below. Save the file somewhere within your website.

ms-database

Connect to Access database File

First we need to connect to database using ODBC, if you are not able to connect and encountered error “could not find driver”, most probably you need to enable “php_pdo_odbc” extension in you PHP.ini file.

odbc_pdo_ext

PHP
1234567891011

$db_username = ''; //username
$db_password = ''; //password
//path to database file
$database_path = "c:/db/path/database.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)}; DBQ=$database_path; Uid=$db_username; Pwd=$db_password;");

Once the PDO object is created, we can now run any SQL queries such as SELECT, UPDATE, INSERT, and DELETE just like MySql, but with some limitation.

SELECT Statement

Let’s say we want to select all records from users table, we can simply run following query and have our records displayed on the browser.

PHP
12345678

$sql  = "SELECT * FROM users";
$result = $database->query($sql);
while ($row = $result->fetch()) {
    echo $row["u_name"];
    echo $row["u_email"];
    echo $row["u_website"];
}

INSERT Statement

To insert the a record, we can run following query. If successful script will display success message or anything goes wrong error will be displayed.

PHP
12345678910

$sql  = "INSERT INTO users(u_name, u_email, u_website, u_message)
VALUES ('saran','[email protected]','http://www.wwebsite.com','hello how are you')";
if($database->query($sql))
{
	echo 'success';
}else{
	$db_err = $database->errorInfo();
	echo 'Error : ('. $db_err[0] .') -- ' . $db_err[2];
}

  • 10 Comments

    Add Comment
    • Suresh
      Fatal error: Uncaught PDOException: could not find driver in C:\xampp\htdocs\aapp\index.php:15 Stack trace: #0 C:\xampp\htdocs\aapp\index.php(15): PDO->__construct('odbc:DRIVER={Mi...') #1 {main} thrown in C:\xampp\htdocs\aapp\index.php on line 15
    • PREET
      Fatal error: Uncaught exception"PDOException'with message 'SQLSTATE[im002] SQL.DriverConnect:0[Microsoft][ODBC Driver Manager] data source error found plz help it does not found database give error in php prog line 8
    • Lim Sochea
      I got an error here: 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:\wamp\www\ODBC\index.php on line 15 Please help
    • Roberto
      Hi, very good the PDO explanatiom, Im having a trouble if the .mdb are out the local machine. I have tryed to map path at local machine but it dosen't connect. If I use c:/mydatabasedir/mydatabase.mdb it Works, but using z:/mydatabase.mdb desen't work. z: is the maped área at another machine. I'm using Windows 7 with wamp/apache/php and the .mdb is at 'z' WINDOWS 2003 SERVER. I opem the machine 'z' for all users but the PDO cant connect. Can you please give me a help? thanks a lot
    • Maycon
      Yes, it's working for me. I thank you very much.
    • Asif
      love it mane.... thank you so much
    • Dulara
      I can read data succesfully from the database but have a problem with executing INSERT INTO queries. what's wrong with it??? :( Error : (42000) -- [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement. (SQLPrepare[0] at ext\pdo_odbc\odbc_driver.c:206)
    • Joshua
      Does It Create Tables
    • Richard MENU
      FOR INSERT, use quote is better : $name = "dog's"; VALUES (" . $database->quote($name) .....
    • Richard MENU
      With new Office pro, replace the line : $database = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; ...... by : $database = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; ....