PHP 8.3.4 Released!

oci_error

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

oci_error最後に見つかったエラーを返す

説明

oci_error(?resource $connection_or_statement = null): array|false

最後に見つかったエラーを返します。

この関数は、エラーが発生した直後にコールしなければなりません。 文の実行が成功すると、エラーはクリアされてしまいます。

パラメータ

connection_or_statement

大半のエラーでは、connection_or_statement は関数コール時に渡されたリソースハンドルとなります。 oci_connect()oci_new_connect()oci_pconnect() の接続エラーの場合は null を渡します。

戻り値

もしエラーが見つからない場合、oci_error()false を返します。それ以外の場合は、 oci_error() はエラーの情報を連想配列で返します。

oci_error() の配列の形式
配列のキー 説明
code int Oracle のエラーコード。
message string Oracle のエラーメッセージ。
offset int SQL 文の中でのエラーが発生した場所のバイト位置。SQL 文がない場合は 0 となります。
sqltext string SQL 文のテキスト。SQL 文がない場合は空文字列となります。

変更履歴

バージョン 説明
8.0.0, PECL OCI8 3.0.0 connection_or_statement は、nullable になりました。

例1 接続エラー後の Oracle エラーメッセージの表示

<?php
$conn
= oci_connect("hr", "welcome", "localhost/XE");
if (!
$conn) {
$e = oci_error(); // oci_connect のエラーの場合、ハンドルを渡しません
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
?>

例2 パースエラー時の Oracle エラーメッセージの表示

<?php
$stid
= oci_parse($conn, "select ' from dual"); // クォートが間違っていることに注意
if (!$stid) {
$e = oci_error($conn); // oci_parse エラーの場合は接続ハンドルを渡します
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
?>

例3 実行エラー時の、 Oracle エラーメッセージと問題となった文の表示

<?php
$stid
= oci_parse($conn, "select does_not_exist from dual");
$r = oci_execute($stid);
if (!
$r) {
$e = oci_error($stid); // oci_execute のエラーの場合、ステートメントハンドルを渡します
print htmlentities($e['message']);
print
"\n<pre>\n";
print
htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
print
"\n</pre>\n";
}
?>

add a note

User Contributed Notes 1 note

up
-31
alvaro at demogracia dot com
9 years ago
Please note that, unlike equivalent functions in other DB extensions, skipping the resource argument is not synonym for "just get last error".
To Top