php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #43443 PDO::prepare() throws PDOException instead of returning FALSE
Submitted: 2007-11-28 23:57 UTC Modified: 2008-11-07 14:45 UTC
From: r dot wilczek at web-appz dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: openSuse 10.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: r dot wilczek at web-appz dot de
New email:
PHP Version: OS:

 

 [2007-11-28 23:57 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-22 20:31 UTC] 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.
 [2008-05-23 07:54 UTC] 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-11-06 10:45 UTC] johannes@php.net
Right, the Exception is the only way we can report an error there.
 [2008-11-06 16:09 UTC] 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-07 14:45 UTC] 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."
 [2010-07-05 07:19 UTC] l dot declercq at nuxwin dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 06:01:29 2024 UTC