How PHP uses PDO to access Oracle databases in a detailed way

Source: Internet
Author: User
Tags php and mysql
Pod extension is added in PHP5, which provides PHP built-in class PDO to access the database, different databases use the same method name, to solve the problem of non-uniform database connection. The following article is mainly about PHP using PDO to access the Oracle database steps, the need for friends can refer to.

Objective

PDO has learned from the outset the successes and failures of existing database extensions. Because PDO's code is brand new, we have the opportunity to start designing performance again to take advantage of the latest features of PHP 5.

PDO is designed to provide the basis for common database functionality while providing easy access to the unique features of the RDBMS.

Although the lamp in PHP and MySQL is a standard pairing, but the company's project is MySQL and Oracle as the runtime and the detailed library, so PHP connection to the Oracle database must be implemented. In this article, if you connect to the Oracle database via PDO, here's a few words, let's take a look at the detailed introduction.

Development environment

Let's also explain the environment I'm using:

    • RedHat (CentOS) 6.7

    • PHP5.6

    • oracle11g

The construction of php5.6 can be referred to here;

Oracle installation in a Linux environment can be found here

Of course, you can also not install the entire Oracle, but only install the instant client, if the instant client's Oracle connection step, see the final acknowledgements section of this blog post.

Before you begin the following steps, it is important to ensure that all 3 of the above environments are properly configured for availability.

Installing PDO_OCI

If you are installing the php5.6.31 version, it seems that the Pdo_oci module has been brought in, but on the safe side, we re-install it again.

Download and Unzip


$ wget https://pecl.php.net/get/pdo_oci-1.0.tgz$ tar-xvf pdo_oci-1.0.tgz$ cd pdo_oci-1.0

Modifying a configuration file

Update the CONFIG.M4 file in the catalog so that it fits oracle11g


# Find code similar to the following on line 10th, add these two lines: Elif test-f $PDO _oci_dir/lib/libclntsh. $SHLIB _suffix_name.11.2; Then pdo_oci_version=11.2# add these lines around line 101th: 11.2) php_add_library (clntsh, 1, pdo_oci_shared_libadd);;

Compile

Execute in current directory


$ phpize

Phpize is similar to the GCC command, which is mainly used to generate PHP plug-ins. Phpize is dependent on the Php-deval module, if the command can not be executed, please use Yum to install php-deval;

You can check if it is installed by using the following command:


$ RPM-QA | grep php

Check to see if there are any php56w-deval-5.6.xxx, and if not, please install it with yum install Php56w-deval.

Installation

After the Phpize command executes, a number of files are generated in the directory, including the standard files for installing the application through the source code in our common Linux configure, etc., by executing the following command in turn:


$./configure$ Make && make install

There is usually a problem here, and when make is prompted in the Pdo_oci.c file:


pdo_oci.c:34:error:expected ' = ', ', ', '; ', ' asm ' or ' attribute ' before ' pdo_oci_functions '

The solution is to modify the 34th line of the Pdo_oci.c file, change the function_entry to Zend_function_entry, save and re-make it.

Create INI file

After success, you will be prompted to generate the Pdo_oci.so module in the/usr/lib64/php/modules directory, all we have to do is tell PHP that we are going to refer to this module.

Create a new INI file under the/ETC/PHP.D directory: Pdo_oci.ini, Content:


Extension=pdo_oci.so

Here, the Pdo_oci module is ready to install, and we can do it by php-m | grep oci to view.

Installing OCI8

The next step is to install the Oci8 module.

Download and Unzip

Execute the following command on the command line, in turn:


$ wget https://pecl.php.net/get/oci8-2.0.8.tgz$ tar-xvf oci8-2.0.8.tgz$ cd oci8-2.0.8

Compiling and installing

The steps and the above Pdo_oci are similar:


$ phpize$./configure--with-oci8=shared$ make && make install

Create INI file

Similarly, we create Oci8.ini in the/ETC/PHP.D directory, content:


Extension=oci8.so

Restart Apache

Don't forget to reboot the Apache server to reload the PHP module:


$ Service httpd Restart

We can create a test.php file in the site directory, the content of the file is:


<?phpphpinfo ();

Then we go to the page to see the printed phpinfo information, to see the PDO, Pdo_oci and oci8 modules in the relevant information.

Summarize

References & Acknowledgements

php5.3 connection to Oracle client and PDO_OCI module installation

Installing PDO_OCI and OCI8 PHP extensions on CentOS 6.4 64bit

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.