PHP and database ODBC

Source: Internet
Author: User
Tags dsn odbc connection

ODBC for PHP Databases

ODBC is an application programming interface (API) that allows you to connect to a data source (such as the MS Access database ).


--------------------------------------------------------------------------------

Create an ODBC connection
Because of ODBC connection, you can connect to any database, on any computer, on your network, as long as an ODBC connection is available.

The following describes how to create an ODBC connection to the MS Access database:

Open the Administrative Tools icon in your control panel.
Double-click the data source (ODBC) icon.
Select the system DSN tag.
Click Add system DSN label.
The Microsoft Access driver. Click Finish.
Click Next to find the database.
Name of the database data source (DSN ).
Click OK.
Note that this configuration requires the location of your website on the computer. If you are running Internet Information Server (IIS) on your computer, the instructions above will work, but if your website is located on a remote server, you must have actual access to the server, or ask your web host to create a DSN for your use.


--------------------------------------------------------------------------------

Connect to an ODBC
The odbc_connect () function is used to connect to the ODBC data source. This function has four parameters: Data Source Name, user name, password, and an optional cursor type.

The odbc_exec () function is used to execute SQL statements.

For example
In the following example, a connection DSN is created with no user name or password. Then it creates a database and executes it:

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

Retrieve records
The odbc_fetch_row () function is used to record the returned result set. This function returns true if it can return rows, otherwise false.

This feature requires two parameters: Result ODBC identification and optional continuous numbers:

 

odbc_fetch_row($rs)
 

Retrieve field records
The odbc_result () function is used to read domain records. This function requires two parameters: the odbc id of the result and a field number or name.

The first field record of the return value below the code line:

 

$compname=odbc_result($rs,1);

The following "company name" is the field of return value for the code line ":

 

$compname=odbc_result($rs,"CompanyName");
 

Closed ODBC connection
The odbc_close () function is used to close ODBC connections.

 

odbc_close($conn);
 

For example, an ODBC
The following example shows how to create a database connection first, then the result set, and then the HTML table of the data.

 

<?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>
 
For more information, see www.111cn.cn/phper/php.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.