#43443 [Opn->Csd]: PDO::prepare() throws PDOException instead of returning FALSE

From: Date: Fri, 07 Nov 2008 14:45:26 +0000
Subject: #43443 [Opn->Csd]: PDO::prepare() throws PDOException instead of returning FALSE
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1456@lists.php.net to get a copy of this message
ID: 43443 Updated by: vrana@php.net Reported By: r dot wilczek at web-appz dot de -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: openSuse 10.3 PHP Version: 5.2.5 New Comment: 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." Previous Comments: ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ [2007-11-28 23:57:14] r dot wilczek at web-appz dot de Description: ------------ Documentation says PDO::prepare() returns FALSE on failure. But tests with PDO_SQLITE and PDO_MYSQL show that you either get a PDOException thrown at you or a PDOStatement which cannot be executed. I am quite surprised, for I upgraded from PHP5.2.0 to PHP5.2.5. In 5.2.0 both drivers threw exceptions on syntax-errors in PDO::prepare(). One could live with exceptions instead of FALSE but it should be done in a uniform manner of all the drivers. Reproduce code: --------------- $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'password', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $stmt = $pdo->prepare('some nonsense'); var_dump($stmt); $pdo = new PDO('sqlite:/tmp/foo.db', 'user', 'password', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $stmt = $pdo->prepare('some nonsense'); var_dump($stmt); Expected result: ---------------- bool(false) bool(false) Actual result: -------------- object(PDOStatement)#2 (1) { ["queryString"]=> string(13) "some nonsense" } Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "some": syntax error' in PHPDocument1:5 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=43443&edit=1

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