Welcome to the Oracle community forum and interact with 2 million technical staff to enter Oracle10gInstantClient (Free Download). It is the easiest way to connect PHP to a remote Oracle database. It only requires three libraries to be installed. The InstantClient library used by PHP to access the current Oracle API is called OCI8. (the name of this C interface is the most
Welcome to the Oracle community forum and interact with 2 million technical staff> accessing the Oracle 10g Instant Client (Free Download) is the easiest way to connect PHP to a remote Oracle database, it only needs to install three libraries. The Instant Client library used by PHP to access Oracle's current API is called OCI8. (the name of this C interface is the most
Welcome to the Oracle community forum and interact with 2 million technical staff> enter
Oracle 10g Instant Client (Free Download) is the easiest way to connect PHP 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. (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
Download "Instant Client Package-Basic" from Oracle Instant Client ". In Linux, you should also download "Instant Client Package-SDK ".
Apache HTTPD Server PHP still recommends Apache 1.3
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.
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.
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.
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.
Edit this environment and add c: \ instantclient10_1 to PATH (before other Oracle Directories ).
For example, on Windows 2000, clickStart> set> Control Panel> system> advanced> environment variable"To 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.
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.
Restart Apache.
To check whether extensions are configured, create a simple PHP script where the web server can read.
Phpinfo ();
?>
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 ).
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 = 8888
Make
Make install
Edit $ HOME/apache/conf/httpd. conf and add:
AddType application/x-httpd-php. php
AddType application/x-httpd-php-source. phps
Download and decompress PHP.
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.
Install RPM as the root user.
Rpm-Uvh oracle-instantclient-basic-10.1.0.3-1.i386.rpm
Rpm-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 ).
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.
Re-build the "configure" script in the top-level PHP Directory.
Cd php-4.3.9
Rm-rf autom4te. cache config. cache
./Buildconf -- force
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
Re-build PHP.
Make
Make install
Copy the PHP configuration to the location specified by -- with-config-file-path.
Cp php. ini-recommended $ HOME/apache/conf/php. ini
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/sh
APACHEHOME =/home/apache
LD_LIBRARY_PATH =/usr/lib/oracle/10.1.0.3/client/lib: $ {LD_LIBRARY_PATH}
TNS_ADMIN =/home
Export LD_LIBRARY_PATH TNS_ADMIN
Echo Starting Apache
$ APACHEHOME/apachectl start
To confirm whether the extension is configured, create a simple PHP script where the web server can read it.
Phpinfo ();
?>
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:
$ Conn = OCILogon ("hr", "hr_password", '// mymachine. mydomain: port/MYDB );
$ Query = 'select table_name from user_tables ';
$ Stid = OCIParse ($ conn, $ query );
OCIExecute ($ stid, OCI_DEFAULT );
While ($ succ = OCIFetchInto ($ stid, $ row )){
Foreach ($ row as $ item ){
Echo $ item ."";
}
Echo"
\ 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.