| |
|
In this tutorial we will learn how to Connecting access / odbc database using PHP This is very simple . For connecting any Access database you have to connect it through ODBC. You need Apache or IIS as web-server, php 4 or later and
Microsoft Data Access Component . MDAC here at the Microsoft Data Access page . (MDAC also comes with Visual basic 6)
Next create an Access database "CompanyData" with the following table
TableName "Emp"
| Fieldname |
Type |
| EmpIid |
Text |
| EmpName |
Text |
Next create the ODBC (Open Database Connectivity) for this database and create a DSN(Data Source Name) for the database.
Just follow these simple steps
- Open the Administrative Tools icon in your Control Panel. (windows XP and 2000, for Win 98 ODBC is in control panel itself).
- Double-click on the Data Sources (ODBC) icon inside.
- Choose the System DSN tab.
- Click on Add in the System DSN tab(2nd from left).
- Select the Microsoft Access Driver .
Click Finish.
- In the next screen, click Select to locate the database.
- Give the database a Data Source Name (DSN) .("testodbc" here)
- Click OK .
In case your database is in remote server then ask your web administrater to create the DSN for you.
Now to connect this ODBC database use the PHP funtion odbc_connect() .
resource odbc_connect ( string dsn, string user, string password [, int cursor_type] )
$conn=odbc_connect('testodbc','',''); //name of DSN is testodbc , password and username both blank
$sql="SELECT * FROM emp";
$rs=odbc_exec($conn,$sql);
Now let us see the full code
$conn=odbc_connect('testodbc','','');
$sql="SELECT * FROM emp";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error occured ");}
echo "<table><tr>";
echo "<th>EmployeeId</th>";
echo "<th>EmployeeName</th></tr>";
while ($d=odbc_fetch_array($rs))
{
$compname=$d['empid'];
$conname=odbc_result($rs,"empname");
echo "<tr><td>$compname</td>";
echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
So , this is how you can connect to Access database using PHP.
Back to Tutorial Index page
Your comments are most welcome admin@koderguru.com
|
|
|