Notes on connecting php to oracle database via php_oci8 extension in linux

Source: Internet
Author: User
Tags php online
In linux, php uses php_oci8 extension to connect to the oracle Database. note: connecting php to oracle is not as simple as connecting to mysql. In most cases, it is not used, however, if you need to use php to access the oracle database, it will be tricky.

The procedure is as follows:

My environment is a linux system (centos 6.7 64-bit), and the wdcp panel is installed. The php version is 5.3. the solutions found on the Internet are as follows, but there are some pitfalls in the middle, I will fill in later: Note that by: sunfei

1. install InstantClient

Instantclient is a simple client for oracle to connect to the database. you do not need to install a 500Moracle client to connect to the oracle Database. it is available in windows and linux versions. Select the desired version to download. you only need two rpm packages, Basic and Devel.

First pitfall: it is best to register an official oracle account for downloading. when downloading, you are prompted to log on. after logging on, you can download it again, because I have downloaded it several times before, installation errors occur because the download is actually a webpage, not a real rpm package. (By: sunfei)

Install # rpm-ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm # rpm-ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm soft link # ln-s/usr/include/oracle/11.2/client64/usr/include/oracle/11.2/client # ln- s/usr/lib/oracle/11.2/client64/usr/lib/oracle/11.2/client

A 32-bit soft link must be created for a 64-bit system. This may be a legacy bug, or problems may occur in subsequent compilation.

Next, let the system find the database file of the oracle client and modify LD_LIBRARY_PATH:

# vi /etc/profile.d/oracle.shexport ORACLE_HOME=/usr/lib/oracle/11.2/client64export LD_LIBRARY_PATH=$ORACLE_HOME/lib

Run source/etc/profile. d/oracle. sh to make the environment variable take effect.

2. install PDO_OCI

I have not installed this extension here. I only installed oci8, and I will directly look at step 3 (by: sunfei)

When connecting to the Internet, it is very simple to install php online via pecl. for details, refer to How to install oracle instantclient and pdo_oci on ubuntu machine.

Download the source PDO_OCI-1.0.tgz file from https://pecl.php.net/package/pdo_oci.

# wget https://pecl.php.net/get/PDO_OCI-1.0.tgz# tar -xvf PDO_OCI-1.0.tgz# cd PDO_OCI-1.0

Since PDO_OCI has not been updated for a long time, you need to edit the config. m4 file in the ODI_OCI-1.0 folder below to make it support 11g:

# Locate the code similar to the following in line 3 and add the two lines: elif test-f $ PDO_OCI_DIR/lib/libclntsh. $ SHLIB_SUFFIX_NAME.11.2; then PDO_OCI_VERSION = 11.2 # add the rows around 101st: 11.2) PHP_ADD_LIBRARY (clntsh, 1, PDO_OCI_SHARED_LIBADD );;

Compile and install pdo_oci extension: (after installation, find this module in/usr/lib64/php/modules/pdo_oci.so)

$ phpize$ ./configure --with-pdo-oci=instantclient,/usr,11.2$ make$ sudo make install

To enable this extension, create a new pdo_oci.ini file under/etc/php. d/with the following content:

extension=pdo_oci.so

Verify that the installation is successful:

# Php-I | grep oci is successfully installed when the following content is displayed:/etc/php. d/pdo_oci.ini, PDO drivers => oci, sqlite or # php-m
3. install OCI8

Download the oci8-2.0.8.tgzsource file from https://pecl.php.net/package/oci8.

The second pitfall: first enter the php extension directory/www/wdlinux/php/include/php/ext on the wdcp panel, download, decompress (by: sunfei)

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

Compile and install the oci8 extension:

Third pitfall: the following phpize cannot be directly entered in the wdcp panel, so use the complete path

/Www/wdlinux/php/bin/phpize

For the same reason, php-config cannot be found. Therefore, add the following code to the configuration line and add it .... /Client64/lib is followed by a space. note: first, use space-with-php-config =/www/wdlinux/php/bin/php-config.

# phpize# ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib# make# make install

To enable this extension, create an oci8.ini file under/etc/php. d/with the following content:

The fourth pitfall: After make install is completed in the previous step, a path is displayed on the screen. This path is the location of the oci8.so file. if the file is available, you can also get php. add configuration in ini, wdcp panel php. the ini path is in/www/wdlinux/etc/php. ini, to the end of the configuration file, plus a line of extension =/www/wdlinux/apache_php-5.3.29/lib/php/extensions/no-debug-non-zts-20090626/oci8.so, I am added to the front of the zend-related configuration, some people say that it cannot be added to the end. you can try it on your own. Restart apache. by now, you have basically succeeded. (By: sunfei)

extension=oci8.so

Verify that the installation is successful:

# php -i|grep oci8/etc/php.d/oci8.ini,oci8oci8.connection_class => no value => no valueoci8.default_prefetch => 100 => 100oci8.events => Off => Offoci8.max_persistent => -1 => -1oci8.old_oci_close_semantics => Off => Offoci8.persistent_timeout => -1 => -1oci8.ping_interval => 60 => 60oci8.privileged_connect => Off => Offoci8.statement_cache_size => 20 => 20OLDPWD => /usr/local/src/oci8-2.0.8_SERVER["OLDPWD"] => /usr/local/src/oci8-2.0.8

Finally, do not forget to restart the anti-web server, such as apache. you can use phpinfo () to ensure that the extension is successfully installed.

4. test the connection

Create testoci. php in the php directory of your web server such as apache:

Fifth pitfall: the code below shows the $ item section. I don't know why I want to use the htmlentities function to handle it. I didn't find it for the first time. later I found Chinese garbled characters, in the end, this function is removed and $ item is directly displayed. In addition, the encoding parameter ZHS16GBK must be added to the oci_connect parameter. (if the encoding cannot be changed, Baidu will not write it ). (By: sunfei)

 \n";while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) { echo "\n"; foreach ($row as $item) { echo " ".($item !== null ? htmlentities($item, ENT_QUOTES) : " ")."\n"; } echo "\n";}echo "\n";?>

You can see the result when you access this page.

The ultimate challenge: you may find that nothing is displayed after executing the above page. you can display the oci error information after the connected line. Specifically, use oci_error (), found that ora-24408 cocould not generate unique server group name is this error, Baidu for a long time, found the solution, in/etc/sysconfig/network, you must configure HOSTNAME = host name (host name is your own name) and add it in/etc/hosts (after 127.0.0.1 localhost, add a space and add) the host name, restart the network service network restart, and run the test file above. (By: sunfei)

Finally, the problem is solved. you may encounter other problems in the actual process, but you still cannot solve them. I can only say that it will always be solved after a while, remember to post the experience after solving the problem for the new (cai) hand (niao) to learn.

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.