Recently, IBM launched a shocking data product DB2 Express C. The performance of this product is similar to that of other DB2 versions, but what is even more eye-catching is that it is completely free of charge. This is a big impact on open-source databases headed by MySQL. I'm afraid LAMP (Linux + Apache + MySQL + PHP) will become LADP (Linux + Apache + DB2 + PHP) in the future. To cope with this change in advance, let's take a look at various PHP methods for operating DB2 Express C.
Over the past 13 years, PHP has developed many database interfaces. This article takes Windows + PHP5.2 as an example to discuss five common methods of operating DB2 Express C in PHP.
Before discussion, let's use the following DB2 SQL statement to create a table, which will be frequently used in this article.
create table mytablea ...{ id int primary key, name varchar(20) not null, age int not null, phone varchar(30) not null, salary int }
|
I. ODBC Mode
Like other databases (SQL Server and Oracle), DB2 Express C also provides ODBC drivers. However, the ODBC driver of DB2 is not packaged with the installer and needs to be downloaded from the IBM website for use. URL: http://www.ibm.com/?works/cn/db2/v9/index_download.html.
PHP introduced odbc api access functions from 3.0.6. All functions that access ODBC APIs start with odbc. Before using these functions, you must create a user or system DB2 ODBC data source on the local machine. 1. The data source name created in this article is mydb2.
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'src = "/files/uploadimg/20070308/1219580 .jpg"> |
Figure 1 |
First, use the odbc_connect function to connect to the mydb2 data source. The odbc_connect function is defined as follows:
Odbc_connect ("Data Source Name", "User Name", "password", "cursor type" [Optional])
|
The following statements connect to mydb2.
$conn = odbc_connect("mydb2", "db2admin", "mypassword",SQL_CUR_USE_ODBC );
|
Db2admin is the default user name for DB2 Express C during installation.
PHP also provides us with another method to connect to the database with buffer. Odbc_pconnect. This method is similar to odbc_connect, but the connection is not released after the current PHP file is executed. If the mydb2 data source is still used next time, the connection will continue. This improves the execution efficiency of Web programs.
Generally, there are two types of database operations: one is to execute SQL statements that do not return results, such as delete, update, and insert, and the other is to execute SQL statements that return results, for example, select.
In the first case, you can use odbc_do for execution.
odbc_do($conn, "delete * from mytable where id > 1000"); odbc_do($conn, "insert into mytable values(2000, 'mike', 30, '12345678', 3000)");
|
In the second case, you can use odbc_exec for execution.
$result = odbc_exec($conn, "select * from mytable where id = 2000");
|
If odbc_exec is successfully executed, the query result is returned.
There are many ways to output $ result. Here I only show how to convert the result into an array. For other methods, see the PHP manual.
Var $ fetch = array (); Fetch = odbc_fetch_array ($ result, 2); // Save the values of each field in the 2nd rows in the array fetch $ Field1 = odbc_result ($ Query_ID, 1 ); $ Field2 = odbc_result ($ Query_ID, "salary "); Print $ field1. ",". $ field2;
|
Odbc_result can be used to obtain the field value based on the field index and field name.
Finally, use odbc_close to close the database connection.
1