From PHP 5.3 onwards:
A new OCI_CRED_EXT flag can be passed as the "session_mode" parameter
to oci_connect(), oci_new_connect() and oci_pconnect().
$c1 = oci_connect("/", "", $db, null, OCI_CRED_EXT);
This tells Oracle to do external or OS authentication, if configured
in the database.
OCI_CRED_EXT can only be used with username of "/" and a empty
password. Oci8.privileged_connection may be On or Off.
OCI_CRED_EXT is not supported on Windows for security reasons.
The new flag may be combined with the existing OCI_SYSOPER or
OCI_SYSDBA modes (note: oci8.privileged_connection needs to be On for
OCI_SYSDBA and OCI_SYSOPER), e.g.:
$c1 = oci_connect("/", "", $db, null, OCI_CRED_EXT+OCI_SYSOPER);
oci_connect
(PHP 5, PECL oci8:1.1-1.2.4)
oci_connect — Oracle サーバへの接続を確立する
説明
他のほとんどの OCI コールで必要な接続 ID を返します。
パラメータ
- username
-
Oracle ユーザ名
- password
-
username に対するパスワード
- db
-
このオプションパラメータには、ローカル Oracle インスタンスの名前か tnsnames.ora における接続先のエントリ名を指定することができる。
指定されない場合、PHP は接続先のデータベースを決定するために環境変数 ORACLE_SID と TWO_TASK を使用して、ローカル Oracle インスタンス名と tnsnames.ora の場所を適宜決定する。
- charset
-
Oracle サーバのバージョン 9.2 以降を使用している場合、新規接続を確立する際に charset パラメータを指定することができます。 Oracleサーバ < 9.2 を使用している場合、このパラメータは無視され、 かわりに環境変数 NLS_LANG が使用されます。
- session_mode
-
このパラメータはバージョン 1.1 から利用可能で、 次の値を受け付ける: OCI_DEFAULT, OCI_SYSOPER, OCI_SYSDBA 。 OCI_SYSOPER もしくは OCI_SYSDBA のいずれかが指定された場合、 この関数は外部のクレデンシャルを利用して、 権限付きの接続を確立しようと試みる。 デフォルトでは権限付きの接続は無効である。有効にするためには、oci8.privileged_connect を On にする必要がある。
返り値
接続 ID、もしくはエラー時は FALSE を返す。
例
例1 oci_connect() の例
<?php
echo "<pre>";
$db = "";
$c1 = oci_connect("scott", "tiger", $db);
$c2 = oci_connect("scott", "tiger", $db);
function create_table($conn)
{
$stmt = oci_parse($conn, "create table scott.hallo (test varchar2(64))");
oci_execute($stmt);
echo $conn . " created table\n\n";
}
function drop_table($conn)
{
$stmt = oci_parse($conn, "drop table scott.hallo");
oci_execute($stmt);
echo $conn . " dropped table\n\n";
}
function insert_data($conn)
{
$stmt = oci_parse($conn, "insert into scott.hallo
values('$conn' || ' ' || to_char(sysdate,'DD-MON-YY HH24:MI:SS'))");
oci_execute($stmt, OCI_DEFAULT);
echo $conn . " inserted hallo\n\n";
}
function delete_data($conn)
{
$stmt = oci_parse($conn, "delete from scott.hallo");
oci_execute($stmt, OCI_DEFAULT);
echo $conn . " deleted hallo\n\n";
}
function commit($conn)
{
oci_commit($conn);
echo $conn . " committed\n\n";
}
function rollback($conn)
{
oci_rollback($conn);
echo $conn . " rollback\n\n";
}
function select_data($conn)
{
$stmt = oci_parse($conn, "select * from scott.hallo");
oci_execute($stmt, OCI_DEFAULT);
echo $conn."----selecting\n\n";
while (oci_fetch($stmt)) {
echo $conn . " [" . oci_result($stmt, "TEST") . "]\n\n";
}
echo $conn . "----done\n\n";
}
create_table($c1);
insert_data($c1); // c1 を使って行を挿入
insert_data($c2); // c2 を使って行を挿入
select_data($c1); // 両方の挿入した結果が返される
select_data($c2);
rollback($c1); // c1 を使ってロールバック
select_data($c1); // どちらの挿入もロールバックされている
select_data($c2);
insert_data($c2); // c2 を使って行を挿入
commit($c2); // c2 を使ってコミット
select_data($c1); // c2 の結果が返される
delete_data($c1); // c1 を使ってテーブル内の全ての行を削除
select_data($c1); // 行は返されない
select_data($c2); // 行は返されない
commit($c1); // c1 を使ってコミット
select_data($c1); // 行は返されない
select_data($c2); // 行は返されない
drop_table($c1);
echo "</pre>";
?>
注意
注意: もし、Oracle インスタントクライアントとPHPを使用する場合、 次に示す簡単な接続ネーミングメソッドを使用することができます: » http://download-west.oracle.com/docs/cd/B12037_01/network.101/b10775/naming.htm#i498306。 基本的にこれはデータベース名として "//db_host[:port]/database_name" を指定できることを意味する。しかし、古いネーミング方法を使用したい場合、 ORACLE_HOME もしくは TNS_ADMIN のいずれかを設定する 必要があります 。
注意: 同じパラメータを使用して 2 回目やそれ以降に oci_connect() がコールされた場合、 最初のコールで返された接続ハンドルを返します。 これは 1 つのハンドルに対して発行されたクエリは、 他のハンドルにも適用されることを意味します。なぜなら、 これらは 同じ ハンドルだからです。 この動作は以下の例 1 で例示されています。 もしトランザクション的にお互い独立した 2 つのハンドルが必要な場合、 oci_new_connect() を使用してください。
注意: PHP 5.0.0 以前では、代わりに ocilogon() を使用しなければなりません。 まだこの名前を使用することができ、下位互換性のため oci_connect() への別名として残されていますが、 推奨されません。
oci_connect
21-Jul-2008 10:16
01-Jul-2008 01:33
If you want to specify a connection timeout in case there is network problem, you can edit the client side (e.g. PHP side) sqlnet.ora file and set SQLNET.OUTBOUND_CONNECT_TIMEOUT. This sets the upper time limit for establishing a connection right through to the DB, including the time for attempts to connect to other services. It is available from Oracle 10.2.0.3 onwards.
In Oracle 11.1, a slightly lighter-weight solution TCP.CONNECT_TIMEOUT was introduced. It also is a sqlnet.ora parameter. It bounds just the TCP connection establishment time, which is mostly where connection problem are seen.
The client sqlnet.ora file should be put in the same directory as the tnsnames.ora file.
21-Jun-2008 12:46
If PHP is built with Oracle 9.2 or greater client libraries, the
charset parameter is used to determine the character set used by the
Oracle client libraries. When PHP is built with older Oracle client
libraries, or if the parameter is not passed, Oracle NLS environment
variables such as NLS_LANG will be used to determine the character
set.
The character set does not need to match that used by the database.
If it doesn't, Oracle will do its best to convert data to and from the
database character set. Depending on the character sets, this may not
which may always be give usable or valid results, and it can reduce
overall performance
Because passing the parameter removes the environment look up, it can
improve connection performance
13-Sep-2006 06:42
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.
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.
