ODBC is an Application Programming Interface (API) that enables us to connect to a data source (such as an MS Access database).
Through an ODBC connection, you can connect to any database on any computer in your network, as long as the ODBC connection is available.
This is how to create an ODBC connection to an MS Access database:
Open the Administrative Tools icon in Control Panel.
Double-click the data source (ODBC) icon.
Select the System DSN tab.
Click Add in the System DSN tab.
Select Microsoft Access Driver . Click Done .
On the next screen, click Select to locate the database.
Give the database a data source name (DSN) .
Click OK .
Please note that this configuration must be completed on the same computer as your website. If your computer is running Internet Information Services (IIS), the above instructions will work, but if your website is on a remote server, you must have physical access to the server or ask your hosting provider to Create DSN.
The odbc_connect() function is used to connect to ODBC data sources. This function has four parameters: data source name, user name, password, and optional pointer type.
The odbc_exec() function is used to execute SQL statements.
The following example creates a connection to a DSN named northwind without a username or password. Then create and execute a SQL statement:
$conn=odbc_connect('northwind','','');$sql="SELECT * FROM customers";$rs=odbc_exec($conn,$sql);
The odbc_fetch_row() function is used to return records from the result set. The function returns true if rows can be returned, false otherwise.
This function takes two parameters: an ODBC result identifier and an optional line number:
odbc_fetch_row($rs)
The odbc_result() function is used to read fields from records. This function takes two parameters: the ODBC result identifier and the field number or name.
The following line of code returns the value of the first field from the record:
$compname=odbc_result($rs,1);
The following line of code returns the value of a field named "CompanyName":
$compname=odbc_result($rs,"CompanyName");
The odbc_close() function closes the ODBC connection.
odbc_close($conn);
The following example shows how to first create a database connection, then create a result set, and then display the data in an HTML table.
<html><body><?php$conn=odbc_connect('northwind','','');if (!$conn){ exit("Connection failed: " . $conn);}$sql="SELECT * FROM customers";$rs=odbc_exec($conn,$sql);if (!$rs){ exit("SQL statement error");}echo "<table><tr>";echo "<th>Companyname</th>";echo "<th>Contactname</th></tr>";while (odbc_fetch_row($rs)){ $compname=odbc_result($rs,"CompanyName"); $ conname=odbc_result($rs,"ContactName"); echo "<tr><td>$compname</td>"; echo "<td>$conname</td></tr>";}odbc_close($conn);echo "</table>";?></body></html>