downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

oci_execute> <oci_define_by_name
Last updated: Fri, 06 Nov 2009

view this page in

oci_error

(PHP 5, PECL oci8 >= 1.1.0)

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

説明

array oci_error ([ resource $source ] )

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

パラメータ

source

ほとんどのエラーに対応するため、 パラメータは適当なリソースハンドルを指定可能。 oci_connect()oci_new_connect() あるいは oci_pconnect() での接続エラーの場合、 パラメータを渡さない。

返り値

もしエラーが見つからない場合、oci_error()FALSE を返す。 oci_error() はエラーを連想配列として返す。 この配列には、Oracle エラーコード code や Oracle エラー文字列 message が含まれる。

変更履歴

バージョン 説明
4.3 エラーの場所と原因となった SQL テキストを示すため、 offsetsqltext も返される配列に含まれる。

例1 接続エラー後、Oracle エラーメッセージを表示する

$conn = @oci_connect("scott", "tiger", "mydb");
if (!$conn) {
  $e = oci_error();   // oci_connect のエラーの場合、ハンドルを渡さない
  echo htmlentities($e['message']);
}

例2 パースエラー後、Oracle エラーメッセージを表示する

$stmt = @oci_parse($conn, "select ' from dual");  // クオートが間違っている事に注意
if (!$stmt) {
  $e = oci_error($conn);  // oci_parse のエラーの場合、接続ハンドルを渡す
  echo htmlentities($e['message']);
}

例3 実行エラー後、 Oracle エラーメッセージと問題となった文を表示する

$r = oci_execute($stmt);
if (!$r) {
  $e = oci_error($stmt); // oci_execute のエラーの場合、ステートメントハンドルを渡す
  echo htmlentities($e['message']);
  echo "<pre>";
  echo htmlentities($e['sqltext']);
  printf("\n%".($e['offset']+1)."s", "^");
  echo "</pre>";
}

注意

注意: PHP バージョン 5.0.0 以前では、代わりに ocierror() を使用しなければなりません。 まだこの名前を使用することができ、下位互換性のため oci_error() への別名として残されていますが、 推奨されません。



add a note add a note User Contributed Notes
oci_error
mlong-php at mlong dot us
03-May-2004 06:45
It seems in 4.3 (and probably other versions), if you call OCIError twice it will fail on the second time (with an empty array). This used to work on previous versions but no longer.

So my prior example of doing if (OCIError won't work and you have to do it like the other contributed example:

$r = @OCIExecute($selectw,OCI_DEFAULT);
if (!$r)
{
 $erra=OCIError($selectw);
 print "Error: ${erra['code']} ${erra['message']}";
}
vi at sh dot nu
04-Nov-2003 09:29
Here's an example of how to get the offset from an Oracle statement that errored:

<?

$conn = OCILogon ("user", "password", "database");

$statement = OCIParse ($conn, "select foo, bar from t1 where id = 1");

OCIExecute ($statement, OCI_DEFAULT);

$error = OCIError ($statement);

if ($error["offset"]) {

        $sqltext = substr ($error["sqltext"], 0, $error["offset"]) .
                '*' .
                substr ($error["sqltext"], $error["offset"]);
        echo $sqltext;
}

?>

Presuming the column "foo" doesn't exist in the table "t1", the above code will produce the following:

PHP Warning:  OCIStmtExecute: ORA-00904: "FOO": invalid identifier
 in test.php on line 7
select *foo, bar from table where id = 1

Note the asterisk next to the word "foo".

This example may seem overly simple, and the error location obvious, but when you have an enormous query, you'll quickly find this functionality very useful.

Daniel Ceregatti

oci_execute> <oci_define_by_name
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites