ODBC Connection Database

Source: Internet
Author: User
Tags odbc odbc connection

PHP operates a database in a number of ways, such as Mysql,mysqli,odbc,pdo. MySQL is the most primitive Extension PHP operation MySQL database. Mysqli I represents improvement, which provides a relatively advanced function, in Extension, it also adds security, which only operates on a particular kind of database, and when you replace other types of databases, you have to use other types of database operations to manipulate the database. It's a lot of trouble to rewrite the code. Is there a method that can be general-purpose, making it possible to write multiple uses at once, compatible with various databases? The answer is certainly yes, that's OBBC and PDO. PDO is a new extension of PHP 5 that operates on a variety of databases, specifically for PHP, similar to Java's JDBC. Let's talk about this later. Now let's talk about ODBC.

  What is ODBC?

ODBC is a software driver system for connecting programming languages to data storage. ODBC is a free open source system, which appeared in 1992, trying to standardize connection methods, such as functionality and configuration, through programming languages and database query access (SQL normalization).

The role of ODBC is to act as an interface or connector, which has a dual design goal: First, for an ODBC system, it acts as a programming language system, and secondly, for a data storage system, it acts as an ODBC system. Therefore, ODBC requires a "programming language for ODBC" driver (such as the PHP-ODBC Library) and a "ODBC for data Storage System" driver (such as the MYSQL-ODBC Library). In addition to the ODBC system itself, ODBC can handle the configuration of the data source, allowing ambiguity between the data source and programming language.

How do I use ODBC?

When using ODBC, PHP development becomes "independent of the database connector." It is for databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server®, ibm®db2®, Sybase, OpenLink Virtuoso, FileMaker, and Microsoft Office ®access®) Use odbc_query() a function like this. You can also use ODBC for CSV and Excel spreadsheets, depending on the correct ODBC driver settings. See below how to use:

1. First ODBC extension and open, through Phpinfo () to view the module and the state is enabled;

  2. Connect to ODBC

The Odbc_connect () function is used to connect to an ODBC data source. The function has four parameters: the data source name, the user name, the password, and the optional pointer type.
The Odbc_exec () function is used to execute SQL statements.

  3. Retrieving records

The 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)

  4. Retrieving fields from a record

  The Odbc_result () function is used to read a field from a record. The function has two parameters: an ODBC result identifier and a field number or name.  the following line of code returns the value of the first field from a record:   $compname =odbc_result ($rs, 1);
The following line of code returns the value of the field named "CompanyName":
$compname =odbc_result ($rs, "CompanyName");

 5. Close the ODBC connection

  The Odbc_close () function is used to close an ODBC connection.  Odbc_close ($conn);

Note: Other operation functions:http://php.net/manual/zh/ref.uodbc.php

  

ODBC instance

The following example shows how to create a database connection first, create a 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"<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>

ODBC Connection Database

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.