Monday, 10 June 2013

How to connect to Microsoft Access database in php

To connect to any ODBC database like MS-Access, php provides odbc_connect method.
Before you connect to any ODBC database you have to create a DSN for that database using Admin.Tools
You follow below steps to create a DSN.

  • Go to Administrative tools in control panel
  • Click on Data Sources (ODBC)
  • Follow steps on screen to create a dsn for database 
  • You can create dsn for any database which provides ODBC API.
Now Let's assume you have created one DSN called mydsn.

Example with code and syntax :-


// Connect to ODBC DB
$myconn=odbc_connect('mydsn','','');


$mysql="SELECT * FROM emp_db";

// Execute a query on ODBC db

$myrecordset=odbc_exec($myconn,$mysql);

//fetch records from recordset from ODBC database
while (odbc_fetch_row($myrecordset))
  {
  $age=odbc_result($myrecordset,"age");
 echo $age;

  }

odbc_close($myconn);
//Close database connection


Above example show how we can connect to ODBC database like MS-Access using dsn and execute query on it.


No comments:

Post a Comment