Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP database ODBC


May 11, 2021 PHP


Table of contents


PHP database ODBC

This section focuses on how PHP uses ODBC to connect to a database, and covers the basic techniques for PHP to manipulate a database using ODBC.

ODBC is an application programming interface (Application Programming Interface, API) that enables us to connect to a data source, such as an MS Access database.


Create an ODBC connection

With an ODBC connection, you can connect to any database on any computer on your network as long as the ODBC connection is available.

This is how to create an ODBC connection to the MS Access database:

  1. Open the Management Tools icon in the control panel.
  2. Double-click on the Data Source (ODBC) icon.
  3. Select the System DSN tab.
  4. Click Add in the System DSN tab.
  5. Select Microsoft Access Driver. Click to complete .
  6. In the next interface, click Select to locate the database.
  7. Create a data source name (DSN) for the database.
  8. Click OK .

Note that this configuration must be done on the computer on which your site is located. If you are running Internet Information Services (IIS) on your computer, the instructions above will take effect, but if your site is on a remote server, you must have physical access to that server, or ask your host provider to establish a DSN for you.


Connect to ODBC

odbc_connect() function is used to connect to the ODBC data source. The function has four parameters: the data source name, the user name, the password, and the optional pointer type.

odbc_exec() function is used to execute SQL statements.

Instance

The following example creates a connection to a DSN named northwind without a user name and password. Then create and execute a SQL statement:

$conn=odbc_connect('northwind','','');
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);


Get the record back

odbc_fetch_row() function is used to return records from the result set. If the row can be returned, the function returns true, otherwise false is returned.

The function has two parameters: the ODBC result identifier and the optional line number:

odbc_fetch_row($rs)


Take the field back from the record

odbc_result () function is used to read fields from records. The function has 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");


Turn off the ODBC connection

odbc_close () function is used to close the ODBC connection.

odbc_close($conn);


ODBC instance

The following example shows how to create a database connection first, then 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("Error in SQL");}
 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> 

By the end of this section, PHP's knowledge of mySQL databases is over, and for more information about MySQL, please refer to the "MySQL Tutorial" at this site!