php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47655 PDO MySQL prepare is allways emulated
Submitted: 2009-03-14 07:46 UTC Modified: 2009-03-15 08:13 UTC
From: info at chromosoft dot be Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2CVS-2009-03-14 (snap) OS: Ubuntu
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at chromosoft dot be
New email:
PHP Version: OS:

 

 [2009-03-14 07:46 UTC] info at chromosoft dot be
Description:
------------
Hi,

The prepare is allways emulated on the mysql DB.
But if you  use mysqli the prepare work correctly.

MySQL : 5.0.51a
PHP : 5.2.4-2ubuntu5.5



Reproduce code:
---------------
Code with PDO :

$dbh = new PDO('mysql:host=localhost;dbname=db', 'root' , '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare('select * from tt where a = ?');
$sth->execute(array(1));
$sth->fetch();
unset($sth);



Code with Mysqli :

$mysqli = new mysqli("localhost", "root", "", "db");
$a = 5;
if ($stmt = $mysqli->prepare("select * from tt where a = ?"))
{
  $stmt->bind_param("i", $a); 
  $stmt->execute();
}
$mysqli->close();

Actual result:
--------------
Log with PDO
30 Connect     root@localhost on db
30 Query       select * from tt where a = '1'
30 Quit 

Log with Mysqli
31 Connect     root@localhost on db
31 Prepare     [1] select * from tt where a = ?
31 Execute     [1] select * from tt where a = 5
31 Quit       


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-14 13:57 UTC] scottmac@php.net
This was intended due to the mysql api not being quite ready when PDO was written. I believe you can use
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

But there might be bugs with this.
 [2009-03-15 06:41 UTC] info at chromosoft dot be
Great, it works. But there is not much documentation on this option. And I never thought that by default it is FALSE by default/


Thanks
 [2009-03-15 08:13 UTC] info at chromosoft dot be
It's correct, now I have another problem with prepare. The system say  
"FATAL ERROR : SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute."

And this on a execute function (not on query or fetch) and not on the first execute on this statement, and the MYSQL_ATTR_USE_BUFFERED_QUERY attribute is turn ON. 

If I turn ON the ATTR_EMULATE_PREPARES the program work correctly but without a real prepare.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC