oci_pconnect

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

oci_pconnectConnect to an Oracle database using a persistent connection

Opis

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 FastCGI/CGI process). See the Persistent Database Connections section for more information.

Parametry

username

The Oracle user name.

password

The password for username.

connection_string

Zawiera instację Oracle, z którą ma połączyć. Może to być » łańcuch Easy Connect lub nazwa połączenia z pliku tnsnames.ora, lub tego pliku dla lokalnej instancji Oracle.

Jeżeli nie określono, PHP użyje zmiennych środowiskowych takich jak TWO_TASK (pod Linuksem) lub LOCAL (pod Windowsem) oraz ORACLE_SID, aby określić instację Oracle, z którą ma połączyć.

Aby użyć metody nazewnictwa Easy Connect, PHP musi być połączony z bibliotekami klienckimi Oracle 10g lub nowszymi. Łańcuch znaków Easy Connect dla Oracle 10g ma postać: [//]nazwa_hosta[:port][/usługa]. Od Oracle 11g składnią jest: [//]nazwa_hosta[:port][/usługa][:typ_serweru][/nazwa_instancji]. Nazwy usług można znaleźć uruchamiając narzędzie Oracle lsnrctl status na maszynie serwera baz danych.

Plik tnsnames.ora może znajdować się w ścieżce wyszukiwania Oracle Net, która zawiera $ORACLE_HOME/network/admin i /etc. Alternatywnie możesz ustawić TNS_ADMIN tak, aby $TNS_ADMIN/tnsnames.ora był odczytywany. Upewnij się, że daemon ma prawo odczytu z tego pliku.

character_set

Określa zestaw znaków używany przez biblioteki Oracle Client. Zestaw znaków nie musi być zgodny z tym używanym przez bazę danych. Jeżeli nie jest zgodny, Oracle zrobi co w jego mocy aby skonwertować dane do i z zestawu znaków bazy danych. Zależnie od zestawów znaków, może nie dać to wyników nadających się do użycia. Konwersja dodaje także pewien narzut czasowy.

Jeżeli nie określono, biblioteki Oracle Client określają zestaw znaków na podstawie zmiennej środowiskowej NLS_LANG.

Przekazanie tego parametru może skrócić czas potrzebny na połączenie.

session_mode

Ten parametr jest dostępny od wersji PHP 5 (PECL OCI8 1.1) i przyjmuje następujące wartości: OCI_DEFAULT, OCI_SYSOPER oraz OCI_SYSDBA. Jeżeli OCI_SYSOPER lub OCI_SYSDBA zostały określone, ta funkcja spróbuje nawiązać uprzywilejowane połączenie używając zewnętrznych danych autoryzacyjnych. Połączenia uprzywilejowane są domyślnie wyłączone. Aby je włączyć, musisz ustawić oci8.privileged_connect na On.

PHP 5.3 (PECL OCI8 1.3.4) wprowadziło tryb OCI_CRED_EXT. Mówi on Oracle, aby użyć autentykacji zewnętrznej lub systemu operacyjnego, która musi być skonfigurowana w bazie danych. Flaga OCI_CRED_EXT może być użyta tylko z nazwą użytkownika "/" i pustym hasłem. oci8.privileged_connect może być ustawiony na On lub Off.

OCI_CRED_EXT może być połączony z trybami OCI_SYSOPER lub OCI_SYSDBA.

OCI_CRED_EXT nie jest wspierane na Windows, ze względów bezpieczeństwa.

Zwracane wartości

Returns a connection identifier or FALSE on error.

Przykłady

Przykład #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.

Notatki

Informacja: 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.

Zobacz też:

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