Working with MS Access using PHP ODBC

Usually we use Mysql when we are working with PHP . But we can use any other database as well while working with php like MS Access , oracle & sql etc. In this article i am explaining how to work with Microsoft Access using php . To work with Microsoft Access we need create ODBC connection with Microsoft Access database in our system or server manually using below steps .
Note : You should have Microsoft Access installed in your system .

Create ODBC Connection to MS Access :

An ODBC connection can connect to any database, on any computer in your network until ODBC connection is unavailable.

Steps to create an ODBC connection to a Microsoft Access Database (Following Windox XP):

  1. Go To Administrative Tools in your Control Panel.
  2. Open Data Sources (ODBC) .
  3. Open System DSN tab.
  4. Click on Add button on right.
  5. Select the Microsoft Access Driver {Driver do Microsoft Access (*.mdb)} from the list of all drivers and Click on Finish button.
  6. In the next screen, click Select to locate the database.
  7. Give the database a Data Source Name (DSN).
  8. Click OK.

Note : This configuration should be done on the computer where you are going to host your web site , i am using my local system. If you are running Internet Information Server (IIS) on your own computer, the instructions above will work, but if your web site is located on any remote server, you should have physical access to that remote server, or you can send request to your web hosting provider to set up a DSN for you to use.

Now you can work with your php program to play with database . I am explaining this using a simple database handling php program using ODBC php functions .

<?php
$conn=odbc_connect(‘testdb’,’username’,’password’);

/* odb_connect is a php function to connect to the database using odbc drivers connection. It will return connection pointer if connected successfully else return false  */

if (!$conn)
{exit(“Connection Failed: ” . $conn);}

$sql=”SELECT * FROM table1″;

/* simple DB query to fetch all data from a test database */
$rs=odbc_exec($conn,$sql);

/* odbc_exec is the function which will execute database query and return the record set other wise false . It need two parameters 1. connection string return value from odbc_connect and database query string.*/

if (!$rs)
{exit(“Error in SQL”);}

while (odbc_fetch_row($rs))
{

/* odbc_fetch_row will return the array of executed query’s result . It need one parameter which is return value of odbc_exec function */

$Address=odbc_result($rs,”Address”);

/* odbc_result will return single column value from a row . it need two parameter one is databse query result set and field name which we are looking for */
$Address=str_replace(‘”‘,”, $Address);
$City=odbc_result($rs,”City”);
$Pin=odbc_result($rs,”Pin”);
$Mobile=odbc_result($rs,”Mobile”);

}
odbc_close($conn);

/* odbc_connect is the function to close the connection according to passed connection object as parameter */

?>

Hope this article will help you to understand the ODBC management using php . Now days mostly Mysql is used with php but MS Access if we need to use anyother database for our application this will help us .

If you have any concern we will happy to read that , Place that in comment . We will happy to see your valuable feedback .

Show Buttons
Hide Buttons