Five ways to operate DB2 Express C using PHP (1) _php tutorial

Source: Internet
Author: User
Tags db2 express db2 express c
Recently IBM has unveiled a shocking data product DB2 Express C. The performance of this product is similar to that of other DB2 versions, and what attracts more attention is that it is completely free. This is the MySQL-led open source database is a small impact, I am afraid that lamp (linux+apache+mysql+php) to become LADP (linux+apache+db2+php). To deal with this change in advance, let's take a look at the various ways PHP operates DB2 Express c.

PHP has evolved a lot of database interfaces in its 13-year development time. This article takes windows+php5.2 as an example to discuss the five types of operations DB2 Express C commonly used in PHP.

Before the discussion, let's build a table using the following DB2 SQL statement, which will be used frequently in this article.

CREATE TABLE Mytablea
... {
ID int PRIMARY KEY,
Name varchar () is not NULL,
The age int is not NULL,
Phone varchar (+) is not NULL,
Salary int
}

first, the ODBC mode

DB2 Express C, like other databases (SQL Server, Oracle), also provides ODBC drivers. However, the ODBC driver for DB2 is not packaged with the installer and needs to be downloaded from the IBM Web site when it is used. The website is as follows: Http://www.ibm.com/developerworks/cn/db2/v9/index_download.html.

PHP has introduced functions to access the ODBC API from 3.0.6. All functions that access the ODBC API begin with ODBC_. Before you use these functions, you must establish a DB2 ODBC data source for a user or system on this computer. As shown in 1. The data source name established 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:

The following is the statement that connects Mydb2.

$conn = Odbc_connect ("Mydb2", "Db2admin", "MyPassword", SQL_CUR_USE_ODBC);

Where Db2admin is the default user name for DB2 Express C at the time of installation.

PHP also provides us with another way to connect to a database with buffering. Odbc_pconnect. This method is similar to Odbc_connect, except that the connection is not released until the current PHP file is executed. If you still use the MYDB2 data source the next time, you will continue to use this connection. This can improve the execution efficiency of the Web program.

There are generally two types of operations on a database, one is to perform SQL that does not return results such as delete, update, and insert, and the other is to execute the SQL statement that returns the result, such as SELECT.

The first case can be performed using ODBC_DO.

Odbc_do ($conn, "Delete * from mytable where ID > 1000");

The second case can be performed using Odbc_exec.

$result = Odbc_exec ($conn, "select * FROM mytable where id = 2000");

If Odbc_exec executes successfully, the result of the query is returned.

There are many ways to output $result, and here I'll just give you a way to convert the results to an array, and see the PHP Manual for other methods.

var $fetch =array ();

$field 1 = odbc_result ($Query _id, 1);
$field 2 = Odbc_result ($Query _id, "salary");
Print $field 1. "," . $field 2;

Where odbc_result can get field values based on the index of the field and the name of the field.

Finally, use Odbc_close to close the database connection.

1

http://www.bkjia.com/PHPjc/446810.html www.bkjia.com true http://www.bkjia.com/PHPjc/446810.html techarticle recently IBM has unveiled a shocking data product DB2 Express C. The performance of this product is similar to that of other DB2 versions, and what attracts more attention is that it is completely free. This ...

  • Related Article

    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.