Microsoft access database is very useful when we creating a software and we need database we can use access to save data like we use SQL Server. Setup database in MS access is very  easy if you have any experience of excel you feel same when you setting up database in MS access. In this post i will explain you how we can create database in Microsoft access and and perform CRUD (Create Read Update Delete) operation on that using PHP Data Object (PDO).

What is PDO?

We mostly use MySQL or MySQLi extensions when we need to access database but in PHP version 5.1 there is a new and better way to do this  using PDO.

“PDO – PHP Data Object is a new light extension to provide fast access to multiple database.”

PDO extension support no of database here is list of database supported by PDO.

  • PDO_DBLIB ( FreeTDS / Microsoft SQL Server / Sybase )
  • PDO_FIREBIRD ( Firebird/Interbase 6 )
  • PDO_IBM ( IBM DB2 )
  • PDO_INFORMIX ( IBM Informix Dynamic Server )
  • PDO_MYSQL ( MySQL 3.x/4.x/5.x )
  • PDO_OCI ( Oracle Call Interface )
  • PDO_ODBC ( ODBC v3 (IBM DB2, unixODBC and win32 ODBC) )
  • PDO_PGSQL ( PostgreSQL )
  • PDO_SQLITE ( SQLite 3 and SQLite 2 )
  • PDO_4D ( 4D )

To find out which PDO driver you have use this code:

If you did not find any driver you need to enable PDO extensions from php ini file. Open your  php.ini file in any text editor and look for PDO extension

remove ( ; ) before extension you want to enable as shown in above.

After enable extension in php.ini you are ready to use PDO.

Step 1. Create database in Microsoft access.

Search for Microsoft access in window search and open it.
Create new database by using shortcut Ctrl+n  or by selecting option from Office Button at top of window.
Now we can create table inside it you can download a sample db file from here.

Step 2. Create connection to Microsoft access db file with php.

Now we are going to create a connection to MS Access file using PHP PDO.

Please not you need to enter your complete location of Ms Access file as i show in above code. We are using PDO odbc driver to access microsoft access database i did not set any Uid or Pwd to my database file if you like you can set userid and pwd to the file.

Step 3.  Insert data to Ms Access using PDO.

Now we are going to insert data to database we use same SQL query we use to insert data

I use try and catch statement in above code to handle error. We can simply add data to database in PDO first we prepare or query and after we execute our code to insert data.

Step 4. Read all data from database of MS Access

In above code we fetch all data from database in Associative array but we more option to fetch data in different format here is list of option.

  • PDO::FETCH_ASSOC: returns an array indexed by column name
  • PDO::FETCH_BOTH (default): returns an array indexed by both column name and number
  • PDO::FETCH_BOUND: Assigns the values of your columns to the variables set with the ->bindColumn() method
  • PDO::FETCH_CLASS: Assigns the values of your columns to properties of the named class. It will create the properties if matching properties do not exist
  • PDO::FETCH_INTO: Updates an existing instance of the named class
  • PDO::FETCH_LAZY: Combines PDO::FETCH_BOTH/PDO::FETCH_OBJ, creating the object variable names as they are used
  • PDO::FETCH_NUM: returns an array indexed by column number
  • PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names

Step 5. Update data in database using PDO

In this code we can update data in database.

Step 6.  Delete data in database using PDO

In above code we can perform delete operation we are using a filter here

$obj->bindParam(‘id’, ‘value by reference’,  ‘parameter/filter’);

to read more about this please follow this link.
this is a basic tutorial to perform CRUD operation over database using PHP PDO.
Some other simple example using PDO

I hope it will help you to learn to connect different database and perform CRUD operation on the database and help you to learn to php PDO.

Happy coding!!.

Posted in: php