中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Develop IBM Cloudscape and DB2 Universal Database applications with PHP

Level: Intermediate

Dan Scott (dan.scott@ca.ibm.com), Product Manager, IBM DB2 Universal Database for Linux, IBM Canada

24 Feb 2005

Learn how to configure IBM? Cloudscape? 10.0 and IBM DB2? Universal Database? (DB2 UDB) Version 8.2 servers for access from PHP 4.x and PHP 5.x. Write database applications using the Unified ODBC extension. Overcome common performance issues due to scrollable cursors and avoid functional limitations in stored procedures. The Unified ODBC extension for PHP offers a common interface for developing PHP applications that connect to databases through an Open Database Connectivity (ODBC) driver. The DB2 client includes an ODBC driver that enables you to access data in Apache Derby, Cloudscape, DB2 UDB for Linux?, UNIX?, and Windows?, and DB2 UDB for iSeries? and zSeries? servers. Even better, you can compile the Unified ODBC extension directly against the DB2 Call Level Interface (DB2 CLI) so that PHP applications can avoid the overhead of an ODBC Driver Manager and talk directly to your target database server

Introducing PHP: Hypertext Processor

PHP: Hypertext Processor (PHP) is a powerful and increasingly popular server-side scripting language for creating Web content. Portability is one of the primary reasons for PHP‘s popularity:

  • You can compile and run PHP on Microsoft Windows, Linux, BSD, Macintosh OS X, and UNIX servers.
  • PHP can be tightly integrated with most popular Web servers, including Apache and Microsoft IIS, or it can be used as a standalone CGI interpreter.
  • The source code for PHP is freely available, and you are free to write and distribute PHP applications for both commercial and non-commercial use.

The open source community that has developed around, and which contributes to, the PHP language is supportive and enthusiastic. As a result of their efforts, the PHP language offers an impressive collection of extensions offering features ranging from XML transforms and on-the-fly generation of images and Adobe Portable Document Format (PDF) files to support for SOAP clients and servers.

After a brief introduction to the PHP scripting language, this article describes how to compile PHP as a dynamically loaded Apache module with DB2 client support. The article concludes with several examples that demonstrate how to connect to, insert data into, and select data from an IBM Cloudscape or IBM DB2 Universal Database server using PHP. This article updates my previous article to cover the major version advances in every aspect of the stack: from PHP 4 to PHP 5, Apache 1.3.x to 2.0.x, DB2 UDB Version 7 to Version 8, and from Linux distributions based on the 2.4 kernel to the 2.6 kernel.

How PHP scripting works

PHP is primarily a server-side scripting language that enables you to embed application logic within HTML pages, or create entire HTML pages from PHP functions. When the Web server receives a request for a PHP page, control passes to the PHP engine. The PHP engine loads the PHP page, executes any PHP functions within the page, then returns the resulting HTML to the Web server.

To call a PHP function within a Web page, you simply embed the PHP functions within the normal HTML source. PHP functions are embedded in HTML-style tags that begin with ?php and end with ?. For example, <?php echo ‘Hello!‘; ?> demonstrates a call to the PHP function echo(). To include more than one function within the PHP tag, separate the functions by a semicolon (;) character.

Of course, you can include many PHP tags in a single PHP script. To demonstrate how easy it is to write Web pages using PHP, here‘s a short script that prints a sequence of numbers starting and ending at arbitrary values.

Confessions from 2001: Sin one
In my previous article I introduced readers to a few questionable PHP coding practices. In my defense, they were the predominant practices at the time, but the state of PHP development and security awareness has advanced dramatically in the last four years. The first convention is that while the PHP interpreter enables you to place <?php ... ?> PHP code escape sequences directly inside HTML, many developers now write a single PHP file within a single <?php .... ?> block and generate HTML as part of the code flow.

Listing 1. Print sequence function: print.php
<?php function print_sequence ($start, $stop, $increment) {  if ($start > $stop) {    return(FALSE);  }  elseif ($increment <= 0) {    return(FALSE);  }  else {    for ($i = $start; $i < $stop; $i = $i + $increment) {      print "$i ";    }  }  return(TRUE);}?><html><head>  <title>Counting from x to y</title></head><body bgcolor="white"><h1>Counting from x to y</h1><p>Counting from 1 to 10 by 1: <?php print_sequence(1, 10, 1)?></p><p>Counting from 2 to 20 by 2: <?php print_sequence(2, 20, 2)?></p></body></html>

Many current PHP applications, in contrast, separate their code into functions and generate HTML from those functions. This approach makes it much easier to untangle the sequence of calls that generate the eventual output for your PHP program. In the following example, we have moved the formerly raw HTML into a function of its own named print_html(), which is called as the only executed statement within the PHP program.


Listing 2. Print sequence function: print_better.php
<?php function print_sequence ($start, $stop, $increment) {  if ($start > $stop) {    return(FALSE);  }  elseif ($increment <= 0) {    return(FALSE);  }  $numbers = "<p>Counting from $start to $stop by $increment: ";  for ($i = $start; $i < $stop; $i = $i + $increment) {    $numbers .= "$i ";  }  $numbers .= "</p>\n";  return($numbers);}function print_html() {  $html = ‘<html><head>  <title>Counting from x to y</title>  </head><body bgcolor="white">  <h1>Counting from x to y</h1>‘  . print_sequence(1, 10, 1)  . print_sequence(2, 20, 2)  . ‘  </body>  </html>‘;    return($html);}echo print_html();?>

Another advantage of combining this approach with collecting PHP functions into modules is that you can easily standardize the header and footer for your PHP pages into a separate module. Changing the look and feel of your complete PHP site can be accomplished by simply modifying the standard header and footer functions in one module instead of altering every individual PHP page on your site. There is no absolute right or wrong method: finding the right balance between encapsulating PHP output into functions and inserting PHP code into HTML templates is a personal choice. You just need to be aware of the advantages of each approach, and follow the approach that best meets your needs.



Back to top


Installing and configuring the DB2 client

This article assumes that you are running a DB2 Universal Database server or an IBM Cloudscape database in Network Server mode. To connect to a DB2 or Cloudscape database server, the Unified ODBC extension for PHP relies on the Call Level Interface (CLI) layer of the DB2 client. The DB2 client must be installed on the same computer as the PHP interpreter. To begin, download the DB2 Runtime Client from the DB2 support site. Ensure you choose the appropriate client for your operating system and ‘bitness‘ level.

Installing the DB2 client on Linux

  1. Install the DB2 client, ensuring that you select the application development headers and libraries.
  2. When you create a DB2 instance, choose a user name and password appropriate for your needs. For the purposes of this article, we will assume the defaults of user db2inst1 with password ibmdb2.
  3. Confirm that you have correctly installed the application development component by ensuring that /home/db2inst1/sqllib/include/ holds files like sqlcli1.h.

Installing the DB2 client on Windows

The PHP project offers precompiled binaries for the Windows operating systems. These binaries include a version of Unified ODBC which has been compiled against the Windows ODBC Driver Manager. This lets us trade off a fraction of our performance for an install of PHP with DB2 and Cloudscape support that requires no compile steps. Install the DB2 client for Windows and proceed to the next step, Setting up a connection to a database.

Setting up a connection to a database

  1. (Linux only): Inherit the DB2 instance environment into your shell session with the following command:
    bash$ source /home/db2inst1/sqllib/db2profile 

  2. Catalog the server you want to connect to. For example, to catalog a DB2 or Cloudscape server named ‘db2air.toronto.ibm.com‘ running on port 1527, issue the following command:
    bash$ db2 CATALOG TCPIP NODE airnode REMOTE db2air.toronto.ibm.com SERVER 1527

    The node name ‘a(chǎn)irnode‘ helps you to distinguish it from other database servers in your catalog.

  3. Catalog the database you want to connect to. For example, to catalog a database called ‘SAMPLE‘ on a database server with the node name ‘a(chǎn)irnode‘, issue the following command:
    • Cloudscape databases: You must specify that the Cloudscape database will perform the authentication:
      bash$ db2 CATALOG DB SAMPLE AT NODE airnode AUTHENTICATION SERVER

    • DB2 databases: DB2 supports a variety of client/server authentication mechanisms. Check with your administrator for the correct authentication value if the default does not work:
      bash$ db2 CATALOG DB SAMPLE AT NODE airnode 

  4. (Windows only): Catalog the database as a System ODBC data source:
    bash$ db2 CATALOG SYSTEM ODBC DATA SOURCE SAMPLE					

  5. Connect to the database to ensure that the connection was properly cataloged:
    bash$ db2 CONNECT TO SAMPLE USER username USING password 



Back to top


Installing and configuring PHP with Apache 2

The PHP community has stated that PHP can be used in production environments with Apache 2, as long as you use the prefork MPM to avoid thread-safety issues. This is good news, because most modern Linux distributions only ship with Apache 2!

Installing and configuring PHP with Apache 2 (Linux)

The PHP project does not offer installable binaries on Linux, so for the latest Unified ODBC support you will have to download and compile the PHP source. Most modern Linux distributions, however, do ship with Apache 2, so these instructions will target an Apache 2 web server. To compile PHP, your Linux distribution must include at least the apache2-devel (httpd-devel on Red Hat Enterprise Linux 4), autoconf, automake, bison, flex, gcc, and libxml2-devel packages.

  1. To connect to DB2 or Cloudscape databases, the Apache 2 Web server must inherit the DB2 client instance environment. Edit /etc/sysconfig/apache2, /etc/sysconfig/httpd, /etc/init.d/apache2, or /etc/init.d/httpd on your system to include the following line:
    source /home/db2inst1/sqllib/db2profile 

  2. Download the source for the latest version of PHP. The version used in testing and developing this article was PHP 5.0.3.
  3. Extract the files from the tarball:
    bash$ tar xjf php-5.0.3.tar.bz2 

  4. Configure the PHP source with IBM DB2 support in Unified ODBC and specify that you are creating a version of PHP for the Apache 2 Web server:
    bash$ cd php-5.0.3 && ./configure --with-ibm-db2=/home/db2inst1/sqllib   --with-apxs2=/usr/sbin/apxs 

  5. Build and install PHP:
    bash$ make && su -c ‘make install‘ 

If all goes well, you will be asked for your root password and your new PHP binaries will be installed in the /usr/local/lib/php/ directory.

If you receive an error message while building the binaries, you may be missing a required development package on your Linux distribution, or you may have specified the incorrect location of your DB2 instance.

If you receive an error message while installing the binaries, PHP may not have understood the Apache configuration files on your distribution of choice. To work around this problem, modify the following line in the Makefile to move install-sapi to the end of the list:


Makefile: original line
install_targets = install-sapi install-cli install-pear   install-build install-headers install-programs 


Makefile: modified line
install_targets = install-cli install-pear install-build \   install-headers install-programs install-sapi 

The PHP make install command tries to update your Apache 2 configuration files on your behalf. However, some Linux distributions heavily modify the configuration files, and in those cases your intervention is required. On SuSE Linux Professional 9.2, for example, you must create a new file named php5.conf in the /etc/apache2/conf.d/ directory and add the following lines:


php5.conf settings
LoadModule php5_module /usr/lib/apache2/libphp5.soAddType application/x-httpd-php php 

Now restart the Apache Web server by issuing the command /etc/init.d/apache2 restart or /etc/init.d/httpd restart, depending on your Linux distribution. If your Linux distribution offers SELinux support, you may be required to modify the SELinux policy or disable SELinux support to enable the Apache user to inherit the instance user‘s environment. For example, on Red Hat Enterprise Linux 4 the Apache Web server runs as the apache user, and the default SELinux policy prevents the apache user from reading the /home/db2inst1/sqllib/db2profile environment script. Issuing the command setenforce 0 disables SELinux temporarily and will enable you to start the Apache Web server.

Installing and configuring PHP with Apache 2 (Windows)

The PHP project makes the life of the Windows developer easy by offering ready-to-deploy binaries with a complete set of extensions. Even better, Unified ODBC support is built into the PHP binary itself. To install PHP on Windows with a default installation of Apache 2:

  1. Download the Windows binaries for the latest version of PHP. The version used in testing and developing this article was PHP 5.0.3. Choose the ZIP file, rather than the Windows installer, as the ZIP file includes the Unified ODBC extension.
  2. Unzip the files. For the purposes of this article, we‘ll assume that you unzipped the files into the C:\php\ directory.
  3. Copy the C:\php\php.ini-recommended file into the C:\php\ directory as a new file named php.ini. The recommended configuration provides a relatively safe set of defaults for your PHP environment.
  4. Edit the C:\Program Files\Apache Group\Apache2\conf\httpd.conf file to add the following lines:
    php5.conf settings
    LoadModule php5_module ‘c:/php/php5apache2.dll‘AddType application/x-httpd-php .phpPHPIniDir ‘c:/php‘ 

  5. Restart Apache.


Back to top


Writing PHP scripts that connect to DB2 Universal Database

To demonstrate some of the basic operations you can perform with PHP and DB2, we‘ll create a set of Web pages to help you manage a database table that contains data on a set of authors. First we will create a table to hold our author data, then write a PHP script that enables us to browse through existing authors. Finally, we will write a script that enables users to add their own author data to the table.

Connecting to a database using PHP

To use this database, we need to insert some data into the table. We could issue some Data Manipulation Language (DML) statements, but because we have PHP installed, we will create and use a simple PHP form for inserting new records in the database. All of the following PHP scripts use the Unified ODBC functions described in the PHP documentation.

Before we can insert data, we must create a database connection within a PHP script. Once we have confirmed that the connection is successful by returning a list of the tables with our user name, we can re-use that connection function within the rest of the scripts that we write.

The syntax for connecting to a database using PHP is as follows:

int odbc_connect() (string dsn, string user, string password [, int cursor_type]);

where:

  • dsn: The name of the database as registered in the DB2 catalog.
  • user: The name of the user that will connect to the database.
  • password: The password for user.
  • cursor_type: Optional arguments to specify cursor behavior.

Creating the author table

The AUTHOR table contains four columns: LAST_NAME, FIRST_NAME, MIDDLE_INITIAL, and AUTHOR_ID, a unique identifier generated by the database server that serves as the primary key of this table. The Data Definition Language (DDL) statement contained in the following PHP script creates this table:

Defining SQL statements
We define the SQL statement as a string variable for two reasons:
  1. Defining all of our SQL statements at the top of the file makes it easier to read the code and modify the ements to tweak our application. This is especially helpful for long SQL statements that include JOIN operations or complex WHERE clauses that might wrap over several lines.
  2. In many database applications we need to repeatedly issue the same statement, sometimes with slightly different values. Declaring the statement as a string variable when you develop your application makes it easier to convert to a production application.

Listing 3. Create the AUTHOR table (create_table.php)
<?php // connect to the database$conn = odbc_connect(‘SAMPLE‘, ‘db2inst1‘, ‘ibmdb2‘);// define our SQL$sql = ‘CREATE TABLE author (last_name VARCHAR(32) NOT NULL,	first_name VARCHAR(32) NOT NULL,	middle_initial VARCHAR(1),	author_id INTEGER GENERATED ALWAYS AS IDENTITY,	PRIMARY KEY (author_id))‘;// issue our SQL statement directlyodbc_exec($conn, $sql);// close the database connectionodbc_close($conn);?>

To create this table, copy the PHP code into a file called create_table.php. Inherit the DB2 instance environment and run the program from the command line by issuing the following commands:

bash$ source /home/db2inst1/sqllib/db2profilebash$ php create_table.php

As you can see, PHP isn‘t just a Web scripting language--you can write convenient command-line scripts as well. Notice that the username and password is exposed in the script. Not only is this a rather insecure practice to follow--if you have to debug a script, you may inadvertently share your password with another developer--it is also quite inconvenient when you have to change your password in every script. Instead, we can create our own module db2lib.php with a common function to connect to our sample database SAMPLE with user name ‘db2inst1‘ and password ‘ibmdb2‘. Then by including db2lib.php, we can use the following function in our scripts:


Listing 4. Simple database connection function: db2lib.php
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用 Apache Derby 開發(fā) Perl 應(yīng)用程序
ADO Connection Strings Examples
PHP如何連接INFORMIX數(shù)據(jù)庫
WIN7 64位旗艦版安裝OFFICE2003 提示:“錯誤1919,配置ODBC數(shù)據(jù)源MS Access Database時發(fā)生錯誤ODEC錯誤”
LAMP (Linux, Apache, MySQL, PHP) HOWTO: Installing MySQL and Apache with PHP support on Linux
Install WordPress 2.9.2 On Ubuntu 10.04
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服