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']}";
}
oci_error
(PHP 5, PECL oci8 >= 1.1.0)
oci_error — Returns the last error found
Descrierea
Returns the last error found.
Parametri
- source
-
For most errors, the parameter is the most appropriate resource handle. For connection errors with oci_connect(), oci_new_connect() or oci_pconnect() do not pass a parameter.
Valorile întroarse
If no error is found, oci_error() returns FALSE. oci_error() returns the error as an associative array. In this array, code consists the oracle error code and message the oracle error string.
Istoria schimbărilor
| Versiunea | Descriere |
|---|---|
| 4.3 | offset and sqltext will also be included in the return array to indicate the location of the error and the original SQL text which caused it. |
Exemple
Example #1 Displaying the Oracle error message after a connection error
$conn = @oci_connect("scott", "tiger", "mydb");
if (!$conn) {
$e = oci_error(); // For oci_connect errors pass no handle
echo htmlentities($e['message']);
}
Example #2 Displaying the Oracle error message after a parsing error
$stmt = @oci_parse($conn, "select ' from dual"); // note mismatched quote
if (!$stmt) {
$e = oci_error($conn); // For oci_parse errors pass the connection handle
echo htmlentities($e['message']);
}
Example #3 Displaying the Oracle error message and problematic statement after an execution error
$r = oci_execute($stmt);
if (!$r) {
$e = oci_error($stmt); // For oci_execute errors pass the statementhandle
echo htmlentities($e['message']);
echo "<pre>";
echo htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
echo "</pre>";
}
Note
Notă: In PHP versions before 5.0.0 you must use ocierror() instead. This name still can be used, it was left as alias of oci_error() for downwards compatability. This, however, is deprecated and not recommended.
oci_error
03-May-2004 06:45
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
