Odbc Connection database, odbc database
Php has many methods to operate databases, such as mysql, mysqli, odbc, and pdo. MySQL is the original Extension for PHP to operate MySQL databases. The I of MySQLi stands for Improvement and provides relatively advanced functions. Extension also increases security, which only operates on specific types of databases, when you replace other types of databases, you have to use the operation methods of other types of databases to operate the database, that is, you have to rewrite the code, which is very troublesome. Is there a general-purpose method that can be used multiple times at a time to be compatible with various databases? The answer is certainly yes, that is, obbc and pdo. Pdo is a new extension of php 5 used to operate various databases. It is dedicated to php and is similar to Java jdbc. I will talk about this later. Let's talk about odbc first.
What is ODBC?
ODBC is a software driver system used to connect programming languages and data storage. ODBC is a free open source system. It appeared in 1992 and tried to standardize connection methods, such as functions and configurations, through programming languages and database query access (SQL Standardization.
ODBC acts as an interface or connector. It has a dual design goal: first, for ODBC systems, it acts as a programming language system. Second, for data storage systems, 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 the data storage system" Driver (such as MySQL-ODBC library ). In addition to the ODBC system itself, ODBC can also process data source configurations, allowing ambiguity between data sources and programming languages.
How to Use odbc?
When odbc is used, PHP development becomes "irrelevant to the Database Connector ". It applies to databases (such as MySQL, PostgreSQL, SQLite, and Microsoft SQL Server)®IBM®DB2®, Sybase, OpenLink kerberoso, FileMaker, and Microsoft Office®Access®) Usage Imageodbc_query()
Such a function. You can also use ODBC for CSV and Excel workbooks, depending on the correct ODBC driver settings. Let's take a look at how to use it:
1. Expand and enable odbc. You can view this module through phpinfo () and its status is enabled;
2. Connect to 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 optional pointer type.
The odbc_exec () function is used to execute SQL statements.
3. Retrieve 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.
// This function has two parameters: ODBC result identifier and optional row number:
odbc_fetch_row($rs)
4. Retrieve fields from records
The odbc_result () function is used to read fields from records. This function has two parameters: ODBC result identifier and field number or name. The following code line returns the value of the first field from the record: $ compname = odbc_result ($ rs, 1 );
The following code line returns the value of the field "CompanyName:
$ Compname = odbc_result ($ rs, "CompanyName ");
5. Close ODBC connection
The odbc_close () function is used to close ODBC connections. 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, create a result set, and then display data in the HTML table.
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>