Doc #43443 [Com]: PDO::prepare() throws PDOException instead of returning FALSE

From: Date: Mon, 05 Jul 2010 05:19:59 +0000
Subject: Doc #43443 [Com]: PDO::prepare() throws PDOException instead of returning FALSE
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-4648@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=43443&edit=1 ID: 43443 Comment by: l dot declercq at nuxwin dot com Reported by: r dot wilczek at web-appz dot de Summary: PDO::prepare() throws PDOException instead of returning FALSE Status: Closed Type: Documentation Problem Package: Documentation problem Operating System: openSuse 10.3 PHP Version: 5.2.5 New Comment: This bug is fixed ? Really ? PHP version PHP 5.2.6 - Driver mysql 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(). On failure, I should get FALSE. The problem is that currently, this method never returns FALSE on failure but a PDOStatement object. $stmt = $db->prepare('something'); if($stmt == FALSE) { echo 'An error occured!'; } else { echo 'I love PHP'; } Expected result: echo 'An error occured!'; Current result: PDOStatement Object ( [queryString] => Something ) ____________________________________ Also, normally, if an error occure 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. Previous Comments: ------------------------------------------------------------------------ [2008-11-07 14:45:26] vrana@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. "If the database server cannot successfully prepare the statement, PDO::prepare() emits PDOException." ------------------------------------------------------------------------ [2008-11-06 16:09:52] sean@php.net I don't see a reason why PDO::prepare() _can't_ return false (that's one of the points of using a factory method). However, I do think the exception is fine, and the docs should be changed. S ------------------------------------------------------------------------ [2008-11-06 10:45:37] johannes@php.net Right, the Exception is the only way we can report an error there. ------------------------------------------------------------------------ [2008-05-23 07:54:23] r dot wilczek at web-appz dot de I agree that the exception makes sense. Well, then either this is a documentation issue (PDO::prepare() never returns FALSE, as mentioned errorneously at http://www.php.net/manual/en/pdo.prepare.php). (At least I am not able to make it returning FALSE) Or PDO::prepare() has to catch PDOExceptions when calling the constructor of PDOStatement and silence them by returning FALSE. ------------------------------------------------------------------------ [2008-05-22 20:31:19] uw@php.net IMHO this is not a bug. This is how PDO is designed. PDO calls the constructor of the PDO_Statement class. The constructor of a class cannot return false. Its only way to indicate an error is to throw an exception. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=43443 -- Edit this bug report at http://bugs.php.net/bug.php?id=43443&edit=1

« previous php.doc.bugs (#4648) next »