There are many ways for PHP to operate the database, such as mysql, mysqli, odbc, pdo, etc. MySQL is the original Extension for PHP to operate the MySQL database. The i in MySQLi stands for Improvement, which provides relatively advanced functions. As far as Extension is concerned, it also increases security. These are only for operating specific types of databases. When you change other types of databases, you have to use other types of databases. To operate the database, you have to rewrite the code, which is very troublesome. Is there a method that can be universal, so that it can be written once and used multiple times, and is compatible with various databases? The answer is of course yes, that is obbc and pdo. pdo is a new extension of PHP 5 for operating various databases. It is exclusive to PHP and is similar to Java's jdbc. More on this later. Now let’s talk about odbc.
What is ODBC?
ODBC is a software driver system used to connect programming languages and data storage. ODBC is a free and open source system that emerged in 1992 and attempts to standardize connection methods such as functionality and configuration through programming languages and database query access (SQL Standardization).
ODBC functions as an interface or connector, and it has dual design goals: first, for the ODBC system, it acts as a programming language system, and secondly, for the data storage system, it acts as an ODBC system. Therefore, ODBC requires a "programming language to ODBC" driver (such as the PHP-ODBC library) and a "ODBC to data storage system" driver (such as the MySQL-ODBC library). In addition to the ODBC system itself, ODBC handles the configuration of data sources, allowing ambiguity between data sources and programming languages.
How to use odbc?
When using odbc, PHP development becomes “database connector agnostic”. It uses functions like odbc_query()
for databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server®, IBM® DB2®, Sybase, OpenLink Virtuoso, FileMaker, and Microsoft Office® Access®. You can also use ODBC with CSV and Excel spreadsheets, depending on the correct ODBC driver settings. Let’s see how to use it:
1. First, odbc is extended and enabled, and the module is viewed through phpinfo() and the status is enabled;
2. Connect to ODBC
odbc_connect() 函数用于连接到 ODBC 数据源。该函数有四个参数:数据源名、用户名、密码以及可选的指针类型。
odbc_exec() 函数用于执行 SQL 语句。
3. Retrieve records
//odbc_fetch_row() 函数用于从结果集中返回记录。如果能够返回行,则函数返回 true,否则返回 false。
//该函数有两个参数:ODBC 结果标识符和可选的行号:
odbc_fetch_row($rs)
4. Retrieve fields from the record
odbc_result() 函数用于从记录中读取字段。该函数有两个参数:ODBC 结果标识符和字段编号或名称。 下面的代码行从记录中返回第一个字段的值: $compname=odbc_result($rs,1<span>);<br /> 下面的代码行返回名为 "CompanyName" 的字段的值:<br /> $compname=odbc_result($rs,"CompanyName");<br /></span>
5. Close the ODBC connection
odbc_close() 函数用于关闭 ODBC 连接。 odbc_close($conn);<br /><br />
Note: Other operating functions: http://php.net/manual/zh/ref.uodbc.php
ODBC instance
The following example shows how to first create a database connection, then create a result set, and then display the data in an HTML table.
<html> <body> <?<span>php </span><span>$conn</span>=<span>odbc_connect</span>('northwind','',''<span>); </span><span>if</span> (!<span>$conn</span><span>){ <br /></span><span> exit</span>("Connection Failed: " . <span>$conn</span><span>);} </span><span> $sql</span>="SELECT * FROM customers"<span>; </span><span> $rs</span>=<span>odbc_exec</span>(<span>$conn</span>,<span>$sql</span><span>); </span><span>if</span> (!<span>$rs</span><span>){ <br /> </span><span>exit</span>("Error in SQL"<span>);} </span><span>echo</span> "<table><tr>"<span>; </span><span>echo</span> "<th>Companyname</th>"<span>; </span><span>echo</span> "<th>Contactname</th></tr>"<span>; </span><span>while</span> (<span>odbc_fetch_row</span>(<span>$rs</span><span>)){ </span><span>$compname</span>=<span>odbc_result</span>(<span>$rs</span>,"CompanyName"<span>); </span><span>$conname</span>=<span>odbc_result</span>(<span>$rs</span>,"ContactName"<span>);<br /> </span><span>echo</span> "<tr><td><span>$compname</span></td>"<span>; </span><span>echo</span> "<td><span>$conname</span></td></tr>"<span>; } </span><span>odbc_close</span>(<span>$conn</span><span>); </span><span>echo</span> "</table>"<span>; </span>?> </body> </html>