PHP and Database odbc_php tutorial

Source: Internet
Author: User
ODBC Database for PHP

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


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

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

Here's how to create an ODBC connection to the MS Access repository:

Open the Administrative Tools icon in your control panel.
Double-click inside the data Source (ODBC) icon.
Select the System DSN label.
Click the Add System DSN tab.
Select the Microsoft Access driver. Click Finish.
In the next screen, click Select to locate the database.
The data source name (DSN) for the database.
Click OK.
Note that this configuration is required to do a location on your Web site on the computer. If you are running Internet Information Server (IIS) on your own computer, the instructions above will work, but if your site is located on a remote server, you must have physical access to the servers, or ask your web host to create a DSN for you to use.


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

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

The Odbc_exec () function is used to execute the SQL statement.

For example
The following example creates a connected DSN for the so-called North Wind, no user name and no password. It then creates a database and executes it:

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

Retrieving records
The Odbc_fetch_row () function is used to record the returned result set. This function returns true if it is able to return a row, otherwise false.

This feature requires two parameters: the ODBC identification of the result and the optional sequential number:

Odbc_fetch_row ($RS)

Retrieving records for a field
The Odbc_result () function is used to read the field records. This feature requires two parameters: The ODBC ID of the result and a field number or name.

Code line below the return value of the first field record:

$compname =odbc_result ($rs, 1);

Code line the value of return below the field of the so-called "Company Name":

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

Closing ODBC connection
the Odbc_close () function is used to close the ODBC connection.

odbc_close ($conn); 
 
 

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

 
!--? 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 "
       
        ", echo "Companyname", echo "Contactname", while (Odbc_fetch_row ($rs)) {$compname =odbc_result ($rs, "  CompanyName ");  $conname =odbc_result ($rs, "ContactName");  echo "$compname"; echo "$conname";} Odbc_close ($conn); echo "";? 
       
 
 
 reprint please specify from: www.111cn.cn/phper/php.html 

http://www.bkjia.com/PHPjc/630900.html www.bkjia.com true http://www.bkjia.com/PHPjc/630900.html techarticle ODBC ODBC for the database of PHP is an application programming interface (API) that allows you to connect to a data source such as MS Access Repository. -------------------------------------------...

  • 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.