Install PHP and Oracle Real-time client

Source: Internet
Author: User
Tags php software
Welcome to the Oracle community forum and interact with 2 million technical staff. Accessing Oracle10gInstantClient is the easiest way for PHP to connect to a remote Oracle database. It only requires three libraries to be installed. The InstantClient library used by PHP to access Oracle's current API is called OCI8.

Welcome to the Oracle Community Forum, interact with 2 million technical staff> accessing the Oracle 10g Instant Client is the easiest way for PHP to connect to a remote Oracle database. It only requires three libraries to be installed. The Instant Client library used by PHP to access Oracle's current API is called OCI8.

Welcome to the Oracle community forum and interact with 2 million technical staff> enter

Oracle 10g Instant Client is the easiest way to connect PHP to a remote Oracle database. It only needs to install three databases.

The Instant Client library used by PHP to access Oracle's current API is called OCI8. (the name of this C interface was first introduced in oracle8 .) PHP Oracle 8 functions can directly call Oracle 8.1.7, 9.x, or 10.x, or use optional abstract classes such as PEAR MDB2 and ADOdb for convenience.

The Instant Client can also use an earlier version of PHP "oracle" extension, but it calls an unsupported Oracle API. We recommend that you do not use this extension for new development in PHP or Oracle.

To use the Instant Client with PHP 4 in Apache, follow these steps. An existing Oracle database is required; Instant Client does not provide an Oracle database. Generally, this database is located on another computer. If the database is located locally, the Oracle component is generally already available, so that the Instant Client is not required.

Software requirements:

Software Notes
Oracle Instant Client Download "Instant Client Package-Basic ". In Linux, you should also download "Instant Client Package-SDK ".
Apache HTTPD Server Apache 1.3 is still recommended in the PHP Field
PHP-PHP hypertext Processor Version 4.3 or later

Enable PHP OCI8 extension on Windows

The Instant Client binary file is a supplement to PHP's Windows pre-built binary file.

  1. Download PHP binary compressed files (not the installer version) and Apache. Install them on the Windows System in the PHP manual. OTN's Open-Source Developer Center contains links to useful background materials, such as "installing Oracle, PHP, and Apache on Windows 2000/XP ", it introduces how to install the traditional and Complete Oracle 10g version (this version is not required for Instant Client ).

    Check whether PHP is running normally before continuing the operation. Oracle Support is not enabled at this stage.

  2. Download the Instant Client Basic Package for Windows from the Instant Client page of OTN. The size of the compressed file is about 30 MB.

  3. Create a sub-directory (for example, c: \ instantclient10_1) and copy the following library from the compressed file:

    • Oraociei10.dll
    • Orannzsbb10.dll
    • Oci. dll

    The total size of these three files is about 80 MB.

    To use an earlier version of the "oracle" extension of PHP (Enabled with "extension = php_oracle.dll" in php. ini), copy ociw32.dll instead of oci. dll.

  4. Edit this environment and add c: \ instantclient10_1 to PATH (before other Oracle Directories ).

    For example, on Windows 2000, choose Start> Settings> Control Panel> system> advanced> environment variables ", edit the PATH in the system Variable list.

    If the tnsnames. ora file is used to define the Oracle Net service name, copy tnsnames. ora to c: \ instantclient10_1 and set the user environment variable TNS_ADMIN to c: \ instantclient10_1. You can also define the default service name in the LOCAL environment variable.

    Set the required Oracle global language environment variables, such as NLS_LANG. If this parameter is not set, the default local environment is used. For more details, see the overview of Oracle PHP application globalization.

    You do not need to set unnecessary Oracle variables, such as ORACLE_HOME and ORACLE_SID.

  5. Edit php. ini and do not set the OCI8 extension as a comment:

    extension=php_oci8.dll

    Set the extension_dir command to the complete PHP extension DLL path. In PHP 4, the DLL is located in the "extensions" subdirectory of the PHP software. In PHP 5, they are located in "ext.

  6. Restart Apache.

To check whether extensions are configured, create a simple PHP script where the web server can read.

 

Use the "http: //" URL to load the script to the browser. The browser page should contain an "OCI8" section showing "oci8 Support enabled.

Enable PHP OCI8 extension on Linux

To add an Oracle connection to Linux, You need to recompile PHP.

The open source code Developer Center contains links to useful background information, such as installing Oracle, PHP, and Apache on Linux, it introduces how to install the traditional and Complete Oracle 10g version (this version is not required for Instant Client ).

  1. Download and install Apache. For example, install it in your home directory:
    cd apache_1.3.31./configure --enable-module=so --prefix=$HOME/apache --with-port=8888makemake install

    Edit $ HOME/apache/conf/httpd. conf and add:

    AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps
  2. Download and decompress PHP.

  3. Download the Basic and SDK Instant Client packages from the Instant Client page on OTN. The total size of the two RPMs is about 30 MB.

  4. Install RPM as the root user.

    rpm -Uvh oracle-instantclient-basic-10.1.0.3-1.i386.rpmrpm -Uvh oracle-instantclient-devel-10.1.0.3-1.i386.rpm

    The first RPM places the Oracle database in/usr/lib/oracle/10.1.0.3/client/lib, the second RPM creates a header in/usr/include/oracle/10.1.0.3/client ).

  5. Back up the patch and apply it to ext/oci8/config. m4 of PHP. The patch line number is based on PHP 4.3.9. If PHP error 31084 has been fixed (probably fixed in PHP 4.3.11 and 5.0.4), this patch is not required.

    If PHP 4.3.9 or 4.3.10 is used, you can save the patch to a file (for example, php_oci8ic_buildpatch) and run the following command to install it:

    patch -u config.m4 php_oci8ic_buildpatch

    This patch creates a new PHP configuration parameter: -- with-oci8-instant-client [= DIR]. on Linux, by default, it uses the latest version of Instant Client installed from RPM. You can specify the directory of the Oracle database to use other versions. In either case, the correct SDK header is automatically used.

    The new parameters are mutually exclusive with the existing -- with-oci8 parameters.

    For example, on a non-Linux platform, extract the Instant Client package to the selected directory. The -- with-oci8-instant-client parameter will need to specify this directory explicitly; for example, -- with-oci8-instant-client =/home/instantclient10_1. The Instant Client SDK should be decompressed to the same directory as the basic package, so that the modified configuration script can find the subdirectory of the header file.

  6. Re-build the "configure" script in the top-level PHP Directory.
    cd php-4.3.9rm -rf autom4te.cache config.cache./buildconf --force
  7. Run configure with the new option. This example uses Apache installed in the main directory.

    ./configure \--with-oci8-instant-client \--prefix=$HOME/php --with-apxs=$HOME/apache/bin/apxs \--enable-sigchild --with-config-file-path=$HOME/apache/conf
  8. Re-build PHP.

    makemake install
  9. Copy the PHP configuration to the location specified by -- with-config-file-path.

    cp php.ini-recommended $HOME/apache/conf/php.ini
  10. Set LD_LIBRARY_PATH to/usr/lib/oracle/10.1.0.3/client/lib and restart Apache.

    If the tnsnames. ora file is used to define the Oracle Net service name, set TNS_ADMIN to the directory containing the file.

    Set all Oracle environment variables before starting Apache. The following script can help with this operation:

    #!/bin/shAPACHEHOME=/home/apacheLD_LIBRARY_PATH=/usr/lib/oracle/10.1.0.3/client/lib:${LD_LIBRARY_PATH}TNS_ADMIN=/homeexport LD_LIBRARY_PATH TNS_ADMINecho Starting Apache $APACHEHOME/apachectl start
To confirm whether the extension is configured, create a simple PHP script where the web server can read it.
 

Use a format similar to "http: // localhost: 8888/ The/phpinfo. php URL loads the script into the browser. The browser page should contain an "OCI8" section showing "oci8 Support enabled.

Connect to Oracle

The Oracle connection information is passed to OCILogon () to create a connection. Tools associated with the Instant Client are usually "away from" any database server, so the Oracle Net connection identifier must be used with the user name and password. Connection information may be well known for established Oracle databases. For new systems, this information is provided by the Oracle installer when installing the database. This installer should configure Oracle Net and create a service name.

In the new database, you may need to unlock the demo mode (such as the HR user) and provide it with a password. You can also connect to SQL * Plus as a SYSTEM user and execute the following statements to complete this operation:

Alter user Username identified by new password account unlock;

There are multiple ways to pass connection information to PHP. The first example uses Oracle 10g Easy Connect syntax to Connect to the HR mode of the MYDB Database Service Running on mymachine. Tnsnames. ora or other Oracle Network files are not required:

$c = OCILogon('hr', 'hr_password', '//mymachine.mydomain/MYDB');

For more information about the Easy Connect syntax, see the documentation on using the Easy Connect naming method in Oracle.

Or, if/home/tnsnames. ora contains:

MYDB =(DESCRIPTION=(ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.mydomain)(PORT = 1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME = MYDB)    )  )

If the TNS_ADMIN environment variable is set to/home (before Apache is started), the connection string can be:

$c = OCILogon('hr', 'hr_password', 'MYDB');

If the environment variable LOCAL (on Windows) or TWO_TASK (on Linux) is set to MYDB, you can use the following code to generate a connection to MYDB:

$c = OCILogon('hr', 'hr_password');
Use Oracle

When the basic connection is available, try to run a simple script testoci. php. Modify the connection details based on your database and load it in your browser. This example lists all tables owned by the user HR:

 \n"; }OCILogoff($conn); ?>
Fault Diagnosis

Oracle PHP troubleshooting FAQs include useful information about connecting to Oracle.

You can download the Oracle SQL * Plus command line tool from the Instant Client page to solve environment and connection problems. For more information, see SQL * Plus Instant Client.

Check whether the environment used by SQL * Plus is the same as that displayed by phpinfo. php.

Windows Help

If the phpinfo. php script does not generate the "OCI8" section showing "oci8 Support enabled", make sure that "extension = php_oci8.dll" is not set as a comment in php. ini.

If the PATH is set incorrectly or the Oracle database cannot be found, start Apache with a warning: "Dynamic Link Library OCI. dll cannot be found in the specified PATH ." The Environment Section on the phpinfo () page displays the PATH value and the Oracle variables actually used by PHP.

If the extension_dir command of php. ini is incorrect, the following warning is displayed when Apache is started: "PHP startup: cannot load dynamic library php_oci8.dll ."

Linux help

Check whether config. m4 is fixed correctly. If "configure" fails, check the config. log file. Restore config. m4, delete the cache file, run./buildconf -- force and configure, and verify whether the problem is related to the change.

Make sure that the timestamp on "configure" is current. Delete All cached files and recreate them if necessary.

Set all necessary Oracle environment variables in the Apache shell.

Conclusion

I hope this article will help you. You can post questions and suggestions on the OTN Instant Client or PHP forum.

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.