PHP SQL ODBC
![](/images/next.gif)
This example illustrates how to create an ODBC Connection for php
application.
Follow the given steps to create an ODBC connection to a MS Access Database:
- Go Control Panel->Administrative Tools
- Double-click on the Data Sources (ODBC) icon inside.
- Select System DSN tab.
- Click on Add.
- Select the Microsoft Access Driver.
- Click Finish.
- Click Select to locate the database.
- Give Data Source Name (DSN).
- Click OK.
|
Now we can use the DSN when connecting to the database. In the given example
below, 'myodbc' is the DSN for the database. Connection from database is created
using
odbc_connect
() and query "SELECT * FROM emp
"
is executed using odbc_exec(). The
result is fetched using
odbc_fetch_row().
Source Code of sqlodbc.php
<html>
<body>
<?php
$conn=odbc_connect('myodbc','','');
if (!$conn){
exit("Connection Failed: " . $conn);
}
$sql="SELECT * FROM emp";
$rs=odbc_exec($conn,$sql);
if (!$rs){
exit("Error in SQL");
}
echo "<table><tr>";
echo "<th>Emp_ID</th>";
echo "<th>Name</th></tr>";
while (odbc_fetch_row($rs)){
$id=odbc_result($rs,"id");
$name=odbc_result($rs,"name");
echo "<tr><td>$id</td>";
echo "<td>$name</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
|
Download Source Code
Output:
![](/tutorialfiles/1/504631.sqlodbc.gif)