php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #27672 oci_error() doesn't return a error if oci_execute() errored.
Submitted: 2004-03-24 08:14 UTC Modified: 2004-03-25 03:17 UTC
From: powerblade at mail dot dk Assigned: tony2001 (profile)
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2004-03-24 (dev) OS: Windows XP
Private report: No CVE-ID: None
 [2004-03-24 08:14 UTC] powerblade at mail dot dk
Description:
------------
When executing a statement it goes as this:

oci_parse() - Validates the SQL statement. However this always returns true so it can't be trusted.
oci_execute() - Executes the query. If anything goes wrong, it simply outputs it to the screen. If you put a @ infront to avoid the warning and then use oci_error() to get the error, you don't get the error string it outputs to the screen.



Reproduce code:
---------------
$query = 'XYZZYX'; /* Invalid SQL string */
$stmt = oci_parse($this->connection, $query);
$aError = oci_error();
if($aError)
{
    throw new DatabaseException("[".$aError['code']."] Can't parse query. ".$aError['message']);			
}
$return = oci_execute($stmt);
if($return === FALSE)
{		
    $aError = @oci_error();
    throw new DatabaseException("[".$aError['code']."] Can't execute query. ".$aError['message']);			
}



Expected result:
----------------
Uncaught exception with the string:
[error code] Can't parse query. [error msg]

or 

[error code] Can't execute query. [error msg]




Actual result:
--------------
Uncaught exception with the string:

[] Can't execute query []

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-24 08:28 UTC] tony2001@php.net
Your code works fine for me.
It returns:
---
Warning: oci_execute() [function.oci-execute.html]: OCIStmtExecute: ORA-00900: invalid SQL statement in /www/index.php on line 27

Fatal error: Uncaught exception 'DatabaseException' with message '[] Can't execute query. ' in /www/index.php:31 Stack trace: #0 {main} thrown in /www/index.php on line 31
---

>oci_parse() - Validates the SQL statement. However this always returns true so it can't be trusted.
no, this is not true.
it does return false, if something went wrong.

>...and then use oci_error() to get the error, you don't get the error string it outputs to the screen.

Please, re-check it twice.
This is false too.

And, please, provide more information about your system.
What version of Oracle & Oracle client do you use?
 [2004-03-24 10:24 UTC] powerblade at mail dot dk
Check your output.

1) The error was first detected by oci_execute(). oci_parse() didn't detect the errornous SQL string.
2) Where is the oracle error message in the exception? I need that info for debugging.
 [2004-03-24 10:40 UTC] tony2001@php.net
Aha, now I see...
It seems to me, that our manual lies about validation.

That's what OCI docs say about oci_parse (OciStmtPrepare() indeed):
--
An application requests a SQL or PL/SQL statement to be prepared for execution using the OCIStmtPrepare() call and passing it a previously allocated statement handle. This is a completely local call, requiring no round trip to the server.
--

oci_parse will return false only if there is some problems with oracle connection.
The only way to validate query is to execute it.

So, I need to change the documentation.
Right?
 [2004-03-24 10:48 UTC] powerblade at mail dot dk
Not only the documentation.
See my #2.

-----

2) Where is the oracle error message in the exception? I need that info
for debugging.

-----

If you look in the exception you get:

-----------------------
Fatal error: Uncaught exception 'DatabaseException' with message '[]
Can't execute query. ' in /www/index.php:31 Stack trace: #0 {main}
thrown in /www/index.php on line 31
--------------------------------

The oracle error message and code is not included in the exception. 
Even though in the code it threw the exception with the following statement:
throw new DatabaseException("[".$aError['code']."] Can't execute
query. ".$aError['message']);	
The only way to grab the oracle error is to set up a custom error handler. You can't use oci_error() to retrieve it.
 [2004-03-24 11:01 UTC] tony2001@php.net
you need to pass $stmt to oci_error(); in this case.
so, your code will look like this:
<?

/* ... */
$query = 'XYZZYX'; /* Invalid SQL string */

$stmt = oci_parse($this->connection, $query);

$return = @oci_execute($stmt);

if($return === FALSE)
{
    $aError = oci_error($stmt); //$stmt here
    throw new DatabaseException("[".$aError['code']."] Can't execute
query. ".$aError['message']);
}

?>

Requalifying as documentation problem and assigning to myself..
 [2004-03-24 11:09 UTC] powerblade at mail dot dk
Just checked it out.
It seems your right. 
It's a documentation problem only. But still enough to confuse some people. :)
You already confused me by changing all the function names a few weeks ago.
 [2004-03-25 02:02 UTC] tony2001@php.net
You can use old names as well.
They are still available as aliases of new functions.
 [2004-03-25 03:17 UTC] tony2001@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:12 UTC] phpdocbot@php.net
Automatic comment on behalf of tony2001
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=4cf9520cf213c37351b45ee9773bd3ff5470faef
Log: fix bug #27672 (oci_parse doesn't validate query indeed, it only prepares and returns statement identifier)
 [2020-12-30 12:47 UTC] nikic@php.net
Automatic comment on behalf of tony2001
Revision: http://git.php.net/?p=doc/ru.git;a=commit;h=fc4a30b3233f68032701088ebba8415b70ac9db5
Log: fix bug #27672 (oci_parse doesn't validate query indeed, it only prepares and returns statement identifier)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Sep 07 23:00:02 2025 UTC