php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52248 PDO::prepare() never return false on failure
Submitted: 2010-07-05 07:29 UTC Modified: 2010-10-25 19:20 UTC
From: l dot declercq at nuxwin dot com Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Linux (Debian Lenny
Private report: No CVE-ID: None
 [2010-07-05 07:29 UTC] 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
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-05 09:03 UTC] 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-09-21 18:18 UTC] 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-10-23 06:46 UTC] kalle@php.net
-Type: Bug +Type: Documentation Problem
 [2010-10-25 19:20 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2010-10-25 19:20 UTC] kalle@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 21:00:02 2026 UTC