oci_pconnect

(PHP 5, PHP 7, PECL OCI8 >= 1.1.0)

oci_pconnectConnect to an Oracle database using a persistent connection

Descrierea

oci_pconnect ( string $username , string $password [, string $connection_string [, string $character_set [, int $session_mode ]]] ) : resource

Creates a persistent connection to an Oracle server and logs on.

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 FPM process). See the OCI8 Connection Handling and Connection Pooling section for more information.

Parametri

username

The Oracle user name.

password

The password for username.

connection_string

Conține instanța Oracle la care se efectuează conectarea. Poate fi un » șir Easy Connect, sau o Denumire de conexiune din fișierul tnsnames.ora, sau denumirea unei instanțe locale Oracle.

Dacă nu este specificat, PHP utilizează variabile de mediu, cum ar fi TWO_TASK (în Linux) sau LOCAL (în Windows) și ORACLE_SID pentru a determina instanța Oracle la care trebuie efectuată conexiunea.

Pentru a utiliza metoda Easy Connect, PHP trebuie să fie legat cu bibliotecile-client Oracle 10g sau ulterioare. Șirul Easy Connect pentru Oracle 10g este de forma: [//]host_name[:port][/service_name]. Pentru Oracle 11g sintaxa este: [//]host_name[:port][/service_name][:server_type][/instance_name]. Denumirile serviciilor pot fi găsite rulând utilitara Oracle lsnrctl status pe mașina unde rulează serverul de baze de date.

Fișierul tnsnames.ora poate fi în calea de căutare Oracle Net, care include $ORACLE_HOME/network/admin și /etc. Ca alternativă, poate fi stabilit TNS_ADMIN astfel încât $TNS_ADMIN/tnsnames.ora este citit. Asigurați-vă că serviciul web posedă dreptul de citire a acestui fișier.

character_set

Determină setul de caractere utilizat de bibliotecile-client Oracle. Setul de caractere nu trebuie în mod obligatoriu să se potrivească cu setul de caractere al bazei de date. Dacă nu se potrivește, Oracle va face tot posibilul să convertească datele din și în setul de caractere al bazei de date. În dependență de seturile de caractere aceasta poate să ducă la rezultate incorecte. Convertirea de asemenea impune un timp mărit de prelucrare.

Dacă nu este specificat, bibliotecile-client Oracle determină setul de caractere din variabila de mediu NLS_LANG.

Transmiterea acestui parametru poate reduce timpul necesar pentru conectare.

session_mode

Acest parametru este disponibil începând cu versiunea PHP 5 (PECL OCI8 1.1) și acceptă următoarele valori: OCI_DEFAULT, OCI_SYSOPER și OCI_SYSDBA. Dacă sunt specificate OCI_SYSOPER sau OCI_SYSDBA, această funcție va încerca să stabilească o conexiune privilegiată utilizând credențiale externe. Conexiunile privilegiate sunt dezactivate implicit. Pentru a le activa trebuie să stabiliți oci8.privileged_connect în On.

PHP 5.3 (PECL OCI8 1.3.4) a introdus valoarea modului OCI_CRED_EXT. Aceasta indică Oracle să utilizeze autentificare Externă sau la nivel de S.O., ceea ce trebuie configurat în baza de date. Fanionul OCI_CRED_EXT poate fi utilizat doar cu numele utilizatorului "/" și o parolă vidă. oci8.privileged_connect poate fi On sau Off.

OCI_CRED_EXT poate fi combinat cu modul OCI_SYSOPER sau OCI_SYSDBA.

OCI_CRED_EXT nu este susținut pe Windows din motive de securitate.

Valorile întoarse

Returns a connection identifier or false on error.

Exemple

Example #1 Basic oci_pconnect() Example using Easy Connect syntax

<?php

// Connects to the XE service (i.e. database) on the "localhost" machine
$conn oci_pconnect('hr''welcome''localhost/XE');
if (!
$conn) {
    
$e oci_error();
    
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid oci_parse($conn'SELECT * FROM employees');
oci_execute($stid);

echo 
"<table border='1'>\n";
while (
$row oci_fetch_array($stidOCI_ASSOC+OCI_RETURN_NULLS)) {
    echo 
"<tr>\n";
    foreach (
$row as $item) {
        echo 
"    <td>" . ($item !== null htmlentities($itemENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo 
"</tr>\n";
}
echo 
"</table>\n";

?>

See oci_connect() for further examples of parameter usage.

Note

Notă: 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: oci8.persistent_timeout, oci8.ping_interval and oci8.max_persistent.

A se vedea și

add a note add a note

User Contributed Notes 2 notes

up
6
php at jaggard dot org dot uk
15 years ago
[Editor's note: OCI8 1.3 should not experience the problem described in this user comment. The first use of such a connection will return an Oracle error which will trigger a cleanup in PHP.  Subsequent persistent connection calls will then succeed.  For high availability you might consider doing consecutive oci_pconnect calls in your script.]

If you connect using oci_pconnect and the connection has logged you off but is still valid, there seems to be no way to re-use that connection. The next time I try oci_pconnect and then perform an oci_execute operation, I get a "ORA-01012: not logged on" warning. This problem remains, even if I close the connection using oci_close. I ended up with the following (rather annoying) code.

<?php
   
function getOracleConnection()
    {
      if (!
function_exists('oci_pconnect'))
        return
false;
     
$toReturn = oci_pconnect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      if (!
function_exists('oci_connect'))
        return
false;
     
$toReturn = oci_connect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      if (!
function_exists('oci_new_connect'))
        return
false;
     
$toReturn = oci_new_connect('user', 'pass', 'db');
      if (
$testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type'))
        if (@
oci_execute($testRes))
          if (@
oci_fetch_array($testRes))
            return
$toReturn;
     
oci_close($toReturn);
      return
false;
    }
?>
up
1
gotankersley at NOSPAM dot com
11 years ago
Installed on CentOS 6.2, and had lots of trouble getting it to recognize tnsnames.ora.  The fix for me was:

1. Make sure apache is getting the TNS_ADMIN env variable by putting it in the /etc/init.d/httpd file:
TNS_ADMIN=/usr/lib/oracle/11.2/client64/network/admin
export PATH TNS_ADMIN

This can be debugging in PHP by <?php echo system('env'); ?> and by verifying that TNS_ADMIN is there.

2. Make sure to use the name at the beginning of the tnsnames.ora file - not the SID (although ideally they should match.  However, if the name at the beginning is XXXX.world then pconnect will expect this - not the SID)
To Top