<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Connect to an Oracle database using a persistent connection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.oci-password-change.html">oci_password_change</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.oci-result.html">oci_result</a></div> <div class="up"><a href="ref.oci8.html">OCI8 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.oci-pconnect" class="refentry"> <div class="refnamediv"> <h1 class="refname">oci_pconnect</h1> <p class="verinfo">(PHP 5, PECL OCI8 >= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_pconnect</span> — <span class="dc-title">Connect to an Oracle database using a persistent connection</span></p> </div> <div class="refsect1 description" id="refsect1-function.oci-pconnect-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>oci_pconnect</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$connection_string</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$character_set</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$session_mode</code></span> ]]] )</div> <p class="para rdfs-comment"> Creates a persistent connection to an Oracle server and logs on. </p> <p class="para"> Persistent connections are cached and re-used between requests, resulting in reduced overhead on each page load; a typical PHP application will have a single persistent connection open against an Oracle server per Apache child process (or PHP FastCGI/CGI process). See the <a href="features.persistent-connections.html" class="link">Persistent Database Connections</a> section for more information. </p> </div> <div class="refsect1 parameters" id="refsect1-function.oci-pconnect-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">username</code></em></span> <dd> <p class="para"> The Oracle user name. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">password</code></em></span> <dd> <p class="para"> The password for <em><code class="parameter">username</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">connection_string</code></em></span> <dd> <p class="para">Contains the <em>Oracle instance</em> to connect to. It can be an <a href="http://download.oracle.com/docs/cd/E11882_01/network.112/e10836/naming.htm" class="link external">» Easy Connect string</a>, or a Connect Name from the <var class="filename">tnsnames.ora</var> file, or the name of a local Oracle instance. </p> <p class="para">If not specified, PHP uses environment variables such as <strong><code>TWO_TASK</code></strong> (on Linux) or <strong><code>LOCAL</code></strong> (on Windows) and <strong><code>ORACLE_SID</code></strong> to determine the <em>Oracle instance</em> to connect to. </p> <p class="para"> To use the Easy Connect naming method, PHP must be linked with Oracle 10<em class="emphasis">g</em> or greater Client libraries. The Easy Connect string for Oracle 10<em class="emphasis">g</em> is of the form: <em class="emphasis">[//]host_name[:port][/service_name]</em>. From Oracle 11<em class="emphasis">g</em>, the syntax is: <em class="emphasis">[//]host_name[:port][/service_name][:server_type][/instance_name]</em>. Service names can be found by running the Oracle utility <em>lsnrctl status</em> on the database server machine. </p> <p class="para"> The <var class="filename">tnsnames.ora</var> file can be in the Oracle Net search path, which includes <var class="filename">$ORACLE_HOME/network/admin</var> and <var class="filename">/etc</var>. Alternatively set <em>TNS_ADMIN</em> so that <var class="filename">$TNS_ADMIN/tnsnames.ora</var> is read. Make sure the web daemon has read access to the file. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">character_set</code></em></span> <dd> <p class="para">Determines the character set used by the Oracle Client libraries. The character set does not need to match the character set used by the database. If it doesn't match, Oracle will do its best to convert data to and from the database character set. Depending on the character sets this may not give usable results. Conversion also adds some time overhead. </p> <p class="para">If not specified, the Oracle Client libraries determine a character set from the <strong><code>NLS_LANG</code></strong> environment variable. </p> <p class="para">Passing this parameter can reduce the time taken to connect. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">session_mode</code></em></span> <dd> <p class="para">This parameter is available since version PHP 5 (PECL OCI8 1.1) and accepts the following values: <strong><code>OCI_DEFAULT</code></strong>, <strong><code>OCI_SYSOPER</code></strong> and <strong><code>OCI_SYSDBA</code></strong>. If either <strong><code>OCI_SYSOPER</code></strong> or <strong><code>OCI_SYSDBA</code></strong> were specified, this function will try to establish privileged connection using external credentials. Privileged connections are disabled by default. To enable them you need to set <a href="oci8.configuration.html#ini.oci8.privileged-connect" class="link">oci8.privileged_connect</a> to <em>On</em>. </p> <p class="para"> PHP 5.3 (PECL OCI8 1.3.4) introduced the <strong><code>OCI_CRED_EXT</code></strong> mode value. This tells Oracle to use External or OS authentication, which must be configured in the database. The <strong><code>OCI_CRED_EXT</code></strong> flag can only be used with username of "/" and a empty password. <a href="oci8.configuration.html#ini.oci8.privileged-connect" class="link">oci8.privileged_connect</a> may be <em>On</em> or <em>Off</em>. </p> <p class="para"> <strong><code>OCI_CRED_EXT</code></strong> may be combined with the <strong><code>OCI_SYSOPER</code></strong> or <strong><code>OCI_SYSDBA</code></strong> modes. </p> <p class="para"> <strong><code>OCI_CRED_EXT</code></strong> is not supported on Windows for security reasons. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.oci-pconnect-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a connection identifier or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.oci-pconnect-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2017"> <p><strong>Example #1 Basic <span class="function"><strong>oci_pconnect()</strong></span> Example using Easy Connect syntax</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// Connects to the XE service (i.e. database) on the "localhost" machine<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_pconnect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">, </span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">, </span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">], </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">), </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$stid </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM employees'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"<table border='1'>\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">oci_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">, </span><span style="color: #0000BB">OCI_ASSOC</span><span style="color: #007700">+</span><span style="color: #0000BB">OCI_RETURN_NULLS</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"<tr>\n"</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$row </span><span style="color: #007700">as </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">" <td>" </span><span style="color: #007700">. (</span><span style="color: #0000BB">$item </span><span style="color: #007700">!== </span><span style="color: #0000BB">null </span><span style="color: #007700">? </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">) : </span><span style="color: #DD0000">"&nbsp;"</span><span style="color: #007700">) . </span><span style="color: #DD0000">"</td>\n"</span><span style="color: #007700">;<br /> }<br /> echo </span><span style="color: #DD0000">"</tr>\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"</table>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> See <span class="function"><a href="function.oci-connect.html" class="function">oci_connect()</a></span> for further examples of parameter usage. </p> </div> <div class="refsect1 notes" id="refsect1-function.oci-pconnect-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Starting with PHP 5.1.2 and PECL OCI8 1.1, the lifetime and maximum number of persistent Oracle connections per PHP process can be tuned by setting the following configuration values: <a href="oci8.configuration.html#ini.oci8.persistent-timeout" class="link">oci8.persistent_timeout</a>, <a href="oci8.configuration.html#ini.oci8.ping-interval" class="link">oci8.ping_interval</a> and <a href="oci8.configuration.html#ini.oci8.max-persistent" class="link">oci8.max_persistent</a>. </span> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.oci-pconnect-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.oci-connect.html" class="function" rel="rdfs-seeAlso">oci_connect()</a> - Connect to an Oracle database</span></li> <li class="member"> <span class="function"><a href="function.oci-new-connect.html" class="function" rel="rdfs-seeAlso">oci_new_connect()</a> - Connect to the Oracle server using a unique connection</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.oci-password-change.html">oci_password_change</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.oci-result.html">oci_result</a></div> <div class="up"><a href="ref.oci8.html">OCI8 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>