Five ways to use PHP to operate DB2 Express C (1)_PHP tutorial

WBOY
Release: 2016-07-15 13:24:12
Original
1078 people have browsed it

Recently, IBM launched a very shocking data product DB2 Express C. The performance of this product is similar to other DB2 versions, and what's even more eye-catching is that it is completely free. This is a big impact on open source databases headed by MySQL. I am afraid that LAMP (Linux+Apache+MySQL+PHP) will become LADP (Linux+Apache+DB2+PHP) in the future. In order to cope with this change in advance, let us first take a look at the various methods of operating DB2 Express C with PHP.

PHP has evolved a lot of database interfaces during its 13 years of development. This article takes Windows+PHP5.2 as an example to discuss the five commonly used methods of operating DB2 Express C in PHP.

Before discussing, let us first use the following DB2 SQL statement to create a table. This table will be used frequently in this article.

<p>create table mytablea<br>...{<br>id int primary key,<br>name varchar(20) not null,<br>age int not null,<br>phone varchar(30) not null,<br>salary int<br>}</p>
Copy after login

1. ODBC method

DB2 Express C, like other databases (SQL Server, Oracle), also provides ODBC drive. However, the ODBC driver of DB2 is not packaged with the installation program and needs to be downloaded from the IBM website when using it. The URL is as follows: http://www.ibm.com/developerworks/cn/db2/v9/index_download.html.

PHP has introduced functions to access the ODBC API starting from 3.0.6. All functions that access the ODBC API begin with odbc_. Before using these functions, a user or system DB2 ODBC data source must be established on the local machine. As shown in Figure 1. The data source name created in this article is mydb2.

Five ways to use PHP to operate DB2 Express C (1)_PHP tutorial
图1

First use the odbc_connect function to connect to the mydb2 data source. The definition of the odbc_connect function is as follows:

<p>odbc_connect ( "数据源名", "用户名", "密码","游标类型"[可选]) </p>
Copy after login

The following is the statement to connect to mydb2.

<p>$conn = odbc_connect("mydb2", "db2admin", "mypassword",SQL_CUR_USE_ODBC );</p>
Copy after login

where db2admin is the default user name of DB2 Express C during installation.

PHP also provides us with another buffered method of connecting to the database. odbc_pconnect. This method is similar to odbc_connect, except that the connection is not released after the current php file is executed. If the mydb2 data source is still used next time, this connection will continue to be used. This can improve the execution efficiency of Web programs.

Generally, there are two types of operations on the database. 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, such as select.

The first case can be executed using odbc_do.

<p>odbc_do($conn, "delete * from mytable where id > 1000");<br>odbc_do($conn, "insert into mytable values(2000, 'mike', 30, '12345678', 3000)"); </p>
Copy after login

The second case can be executed using odbc_exec.

<p>$result = odbc_exec($conn, "select * from mytable where id = 2000");</p>
Copy after login

If odbc_exec is executed successfully, the result of the query is returned.

There are many ways to output $result. Here I only give the method of converting the result into an array. For other methods, please refer to the PHP manual.

<p>var $fetch=array();<br>fetch=odbc_fetch_array($result, 2); // 将第2行的每个字段的值保存在数组fetch中 <br>$field1 = odbc_result($Query_ID, 1);<br>$field2 = odbc_result($Query_ID, "salary");<br>print $field1 . "," . $field2;</p>
Copy after login

Among them, odbc_result can get the field value based on the index of the field and the name of the field.

Finally use odbc_close to close the database connection.

1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446810.htmlTechArticleRecently, IBM launched a very shocking data product DB2 Express C. The performance of this product is similar to other DB2 versions, and what's even more eye-catching is that it is completely free. This...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!