When you are using Oracle 9.2+ I would say that you MUST use the CHARSET parameter.
Of course, you will not notice it until there is accented character... so just specify it and you will avoid a big headache.
So for example here is our Oracle internal conf:
select * from nls_database_parameters;
PARAMETER VALUE
------------------------------ ----------------------------------------
…
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_ISO_CURRENCY AMERICA
NLS_CHARACTERSET WE8ISO8859P15
…
And there our oci_connect call:
$dbch=ocilogon($user,$pass,$connectString,"WE8ISO8859P15");
Without that, you will get question mark (inversed), squares… instead of most accented character.
Don’t forget to use that for writing as well as for reading.
oci_connect
(PHP 5, PECL OCI8 >= 1.1.0)
oci_connect — Connect to an Oracle database
Beschreibung
$username
, string $password
[, string $connection_string
[, string $character_set
[, int $session_mode
]]] )Returns a connection identifier needed for most other OCI8 operations.
See Connection Handling for general information on connection management and connection pooling.
From PHP 5.1.2 (PECL OCI8 1.1) oci_close() can be used to close the connection.
The second and subsequent calls to oci_connect() with the same parameters will return the connection handle returned from the first call. This means that transactions in one handle are also in the other handles, because they use the same underlying database connection. If two handles need to be transactionally isolated from each other, use oci_new_connect() instead.
Parameter-Liste
-
username -
The Oracle user name.
-
password -
The password for
username. -
connection_string -
Beinhaltet die Oracle-Instanz, mit der verbunden werden soll. Dies kann eine » Easy Connect Zeichenkette, ein Connect-Name aus der Datei tnsnames.ora oder der Name einer lokalen Oracle-Instanz sein.
Wenn dies nicht angegebe wird, verwendet PHP Umgebungsvariablen wie
TWO_TASK(unter Linux),LOCAL(unter Windows) oderORACLE_SIDum die Oracle-Instanz mit der verbunden werden soll zu ermitteln.Um die benannte Easy Connect Methode zu verwenden, muss PHP gegen die Clientbibliotheken von Oracle 10g oder höher gelinkt sein. Die Easy Connect Zeichenfolge für Oracle 10g hat den Aufbau: [//]host_name[:port][/service_name]. Für Oracle 11g ist die Syntax: [//]host_name[:port][/service_name][:server_type][/instance_name]. Service name kann bestimmt werden, indem man das Oracle-Hilfswerkzeug lsnrctl status auf dem Datenbankserver ausführt.
Die Datei tnsnames.ora kann sich im Oracle Net Suchpfad befinden, welcher $ORACLE_HOME/network/admin sowie /etc beinhaltet. Alternativ kann TNS_ADMIN auf einen Wert gesetzt werden, damit $TNS_ADMIN/tnsnames.ora gelesen wird. Es muss sichergestellt sein, dass der Webserver Leseberechtigung für die Datei besitzt.
-
character_set -
Bestimmt den Zeichensatz, der von den Oracle Client Bibliotheken verwendet wird. Dieser Zeichensatz muss nicht dem von der Datenbank verwendeten Zeichensatz entsprechen. Wenn diese nicht übereinstimmen, wird Oracle bestmöglich versuchen, die Daten in den Datenbankzeichensatz zu übersetzen. Abhängig von den Zeichensätzen kann dies zu unbrauchbaren Ergebnissen führen. Diese Konvertierung führt auch zu einem erhöhten Zeitbedarf.
Falls nicht angegeben, werden die Oracle Client Bibliotheken versuchen, den Zeichensatz aus der Umgebungsvariablen
NLS_LANGauszulesen.Die Übergabe dieses Parameters verringert die Zeit für den verbindungsaufbau.
-
session_mode -
Dieser Parameter ist seit PHP 5 (PECL OCI8 1.1) verfügbar und akzeptiert die folgenden Werte:
OCI_DEFAULT,OCI_SYSOPERundOCI_SYSDBA. Falls entwederOCI_SYSOPERoderOCI_SYSDBAangegeben wurden wird diese Funktion versuchen die priviligierte Verbindung mit externen Credentials aufzubauen. Um diese einzuschalten muss man oci8.privileged_connect auf On setzen.PHP 5.3 (PECL OCI8 1.3.4) hat den Wert
OCI_CRED_EXTeingeführt. Dies konfiguriert Oracle dazu eine externe oder Os-Authentifizierung vorzunehmen, welche in der Datenbank konfiguriert werden muss. Der SchalterOCI_CRED_EXTkann nur mit dem Benutzernamen "/" und einem leeren Passwort verwendet werden. oci8.privileged_connect kann entweder On oder Off sein.OCI_CRED_EXTkann mitOCI_SYSOPERoderOCI_SYSDBAkombiniert werden.OCI_CRED_EXTist auf Windows-Systemen aus sicherheitsgründen nicht unterstützt.
Rückgabewerte
Returns a connection identifier or FALSE on error.
Beispiele
Beispiel #1 Basic oci_connect() using Easy Connect syntax
<?php
// Connects to the XE service (i.e. database) on the "localhost" machine
$conn = oci_connect('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($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Beispiel #2 Basic oci_connect() using a Network Connect name
<?php
// Connects to the MYDB database described in tnsnames.ora file,
// One example tnsnames.ora entry for MYDB could be:
// MYDB =
// (DESCRIPTION =
// (ADDRESS = (PROTOCOL = TCP)(HOST = mymachine.oracle.com)(PORT = 1521))
// (CONNECT_DATA =
// (SERVER = DEDICATED)
// (SERVICE_NAME = XE)
// )
// )
$conn = oci_connect('hr', 'welcome', 'MYDB');
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($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Beispiel #3 oci_connect() with an explicit character set
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE', 'AL32UTF8');
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($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : " ") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
Beispiel #4 Using multiple calls to oci_connect()
<?php
$c1 = oci_connect("hr", "welcome", 'localhost/XE');
$c2 = oci_connect("hr", "welcome", 'localhost/XE');
// Both $c1 and $c2 show the same PHP resource id meaning they use the
// same underlying database connection
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
function create_table($conn)
{
$stmt = oci_parse($conn, "create table hallo (test varchar2(64))");
oci_execute($stmt);
echo "Created table<br>\n";
}
function drop_table($conn)
{
$stmt = oci_parse($conn, "drop table hallo");
oci_execute($stmt);
echo "Dropped table<br>\n";
}
function insert_data($connname, $conn)
{
$stmt = oci_parse($conn, "insert into hallo
values(to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
oci_execute($stmt, OCI_DEFAULT);
echo "$connname inserted row without committing<br>\n";
}
function rollback($connname, $conn)
{
oci_rollback($conn);
echo "$connname rollback<br>\n";
}
function select_data($connname, $conn)
{
$stmt = oci_parse($conn, "select * from hallo");
oci_execute($stmt, OCI_DEFAULT);
echo "$connname ----selecting<br>\n";
while (oci_fetch($stmt)) {
echo " " . oci_result($stmt, "TEST") . "<br>\n";
}
echo "$connname ----done<br>\n";
}
create_table($c1);
insert_data('c1', $c1); // Insert a row using c1
sleep(2); // sleep to show a different timestamp for the 2nd row
insert_data('c2', $c2); // Insert a row using c2
select_data('c1', $c1); // Results of both inserts are returned
select_data('c2', $c2); // Results of both inserts are returned
rollback('c1', $c1); // Rollback using c1
select_data('c1', $c1); // Both inserts have been rolled back
select_data('c2', $c2);
drop_table($c1);
// Closing one of the connections makes the PHP variable unusable, but
// the other could be used
oci_close($c1);
echo "c1 is $c1<br>\n";
echo "c2 is $c2<br>\n";
// Output is:
// c1 is Resource id #5
// c2 is Resource id #5
// Created table
// c1 inserted row without committing
// c2 inserted row without committing
// c1 ----selecting
// 09-DEC-09 12:14:43
// 09-DEC-09 12:14:45
// c1 ----done
// c2 ----selecting
// 09-DEC-09 12:14:43
// 09-DEC-09 12:14:45
// c2 ----done
// c1 rollback
// c1 ----selecting
// c1 ----done
// c2 ----selecting
// c2 ----done
// Dropped table
// c1 is
// c2 is Resource id #5
?>
Anmerkungen
Hinweis:
An incorrectly installed or configured OCI8 extension will often manifest itself as a connection problem or error. See Installing/Configuring for troubleshooting information.
Hinweis:
In PHP versions before 5.0.0 use ocilogon() instead. Der alte Funktionsname kann noch immer verwendet werden, dies ist jedoch veraltet und wird nicht empfohlen.
Siehe auch
- oci_pconnect() - Connect to an Oracle database using a persistent connection
- oci_new_connect() - Connect to the Oracle server using a unique connection
- oci_close() - Schließt eine Oracle-Verbindung
oci_connect
13-Sep-2006 06:42
24-Jul-2006 06:30
For use PHPv5 functions in PHPv4 i use simple script:
<?php
$funcs=array(
'oci_connect'=>'OCILogon',
'oci_parse'=>'OCIParse',
'oci_execute'=>'OCIExecute',
'oci_fetch'=>'OCIFetch',
'oci_num_fields'=>'OCINumCols',
'oci_field_name'=>'OCIColumnName',
'oci_result'=>'OCIResult',
'oci_free_statement'=>'OCIFreeStatement',
);
// yoy can add yours pairs of funcs.
foreach ($funcs as $k=>$v)
{
if (!function_exists($k))
{
$arg_string='$p0';
for ($i=1;$i<20;$i++) {
$arg_string.=',$p'.$i;
}
eval ('function '.$k.' () {
list('.$arg_string.')=func_get_args();
return '.$v.'('.$arg_string.');
}
');
}
}
?>
simple, but it work. :-)
07-Nov-2005 02:08
lost oracle connection. need restart apache?
Temporarely you can prevent 'connection lost' by using folowing script (use it at your own risk):
<?php
$rnum=rand(0,99999999);
$dbcon = oci_new_connect('XXXXX', 'XXXXXX',
'
(DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(HOST = XXX.XXX.XXX.XXX)
(PORT = 1521)
(HASH = '.$rnum.')
)
(CONNECT_DATA =(SID = XXX))
)
');
?>
07-Nov-2005 09:44
This note is an addendum to note#58378
Seems to be a good workaround set the oracle_home and/instead of the tns_admin.
tnsnames.ora must to be located in
$ORACLE_HOME/network/admin
and in
$TNS_ADMIN/ (if you use it)
---
Best Regards,
Domenico
02-Nov-2005 11:44
Using tnsnames.ora
Apache 2
php 5.0.5
Oracle 10 IstantClient
PHP half of times return this error:
OCISessionBegin: ORA-24327: need explicit attach before authenticating a user in ...
In Oracle manual I find:
ORA-24327 need explicit attach before authenticating a user
Cause: A server context must be initialized before creating a session.
Action: Create and initialize a server handle.
I resolved using Easy Connect Naming Method.
Notice of this problem in bug#29779.
---
Best Regards,
Domenico
28-Oct-2005 02:19
Our tnsnames.ora uses the SERVICE_NAME=mydb - which for some reason wont work with PHP even though it works fine with tnsping. Using SID=mydb worked and a connection was established.