Doc #52248 [Opn->Csd]: PDO::prepare() never return false on failure
| From: | kalle@php.net | Date: | Mon, 25 Oct 2010 17:20:38 +0000 |
| Subject: | Doc #52248 [Opn->Csd]: PDO::prepare() never return false on failure | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-5409@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52248&edit=1
ID: 52248
Updated by: kalle@php.net
Reported by: l dot declercq at nuxwin dot com
Summary: PDO::prepare() never return false on failure
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: Linux (Debian Lenny
PHP Version: Irrelevant
-Assigned To:
+Assigned To: kalle
Block user comment: N
New Comment:
Closed as per reporters comment.
For future reference; This is intented behaviour because although the
SQL may be invalid, it still passes the validation for prepared
statements but fails when executing.
Previous Comments:
------------------------------------------------------------------------
[2010-09-21 18:18:01] markskilbeck@php.net
Automatic comment from SVN on behalf of markskilbeck
Revision: http://svn.php.net/viewvc/?view=revision&revision=303646
Log: #52248 {} is *not* deprecated
------------------------------------------------------------------------
[2010-07-05 09:03:04] l dot declercq at nuxwin dot com
Ok, you can close this thread.
In fact, it's because by default, PDO emulate the Prepared Queries. I
think you should adds an advertisement in the documentation about this
issue.
To resume, if you don't want use exception for error handling and get
FALSE on failure, just set the ATTR_EMULATE_PREPARES attribute to FALSE.
But, don't use that with Mysql server version prior 5.1.17 because the
Prepared queries can't be cached.
Sorry for my poor English, I'm french.
------------------------------------------------------------------------
[2010-07-05 07:29:35] l dot declercq at nuxwin dot com
Description:
------------
hello everyone ;
I have found the following closed ticket:
http://bugs.php.net/bug.php?id=43443&thanks=3
but:
In the current documentation
(http://www.php.net/manual/en/pdo.prepare.php), I see the following
about error handling:
....................
If the database server successfully prepares the statement,
PDO::prepare() returns a PDOStatement object. If the database server
cannot successfully prepare the statement, PDO::prepare() returns FALSE
or emits PDOException (depending on error handling).
....................
So, normally, If I don't use Exception for error handling, I should test
the returned value of the PDO::prepare() method. On failure, I should
get FALSE. The problem is that currently, this method never returns
FALSE on failure but a PDOStatement object that can't be executed.
Also, normally , if an error occur during Sql statement preparation, I
should fetch error from PDO object and not from the PDOStatement object
but currently, I have not error reported from the PDO object:
$db->errorInfo()
Array
(
[0] => 00000
)
The error information are in the PDOStatement :
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'Something' at line 1
)
So, in my opinion, the current documentation for PDO::prepare() method
is wrong!
Thank
Note: Sorry for my poor English, I'm French.
Test script:
---------------
$stmt = $db->prepare('something');
if($stmt == FALSE) {
echo 'An error occured!';
} else {
echo 'I love PHP';
}
Expected result:
----------------
An error occurred!
Actual result:
--------------
I love PHP
And the content of the $stmt var:
PDOStatement Object
(
[queryString] => Something
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52248&edit=1