Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling PDO::ATTR_EMULATE_PREPARES causing 'unknown' issue

Tags:

php

mysql

pdo

Just a quick question regarding PDO's ATTR_EMULATE_PREPARES attribute- simply put, while left on default (true) everything works fine and dandy. Disable it however and, well, I don't even get a PHP error message, just a browser warning telling me that "the connection was reset".

For reference here is a sample of the code I was using

<?php
include_once("config.php");

try {
  $dbh = new PDO
  (
    "mysql:host=". DB_SERVER .";dbname=" . DB_NAME,
    DB_USER,
    DB_PASS,
    array
    (
      PDO::ATTR_PERSISTENT => true,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES => true
    )
  );
} catch(PDOException $e) {
  echo "<pre>";
  print_r("Error: " . $e);
  echo "</pre>";
  die();
}

$idNum = "1";

$sth = $dbh->prepare("SELECT * FROM `table` WHERE `id` = ?;");
$sth->bindParam(1,$idNum);
$sth->execute();
$res = $sth->fetch();
?>

<pre>
<?=print_r($res); ?>
</pre>

Which nicely returns the query from my lovely test table...

Array
(
    [id] => 1
    [field1] => q12w3e4r5t6y7u8i9
    [field2] => kijhgbfvcdoikujyh
)

However were I to have the temerity to set the value of PDO::ATTR_EMULATE_PREPARES to false it would simply fail, and fail again until I return it to its original value. Is there anything I can do to find out what is causing this or have I missed something really simple?

My PHP version is currently 5.4.3 and MySQL is 5.5.24

like image 628
Lucas Avatar asked Nov 16 '12 07:11

Lucas


1 Answers

This looks to be a bug in certain PHP versions:

https://bugs.php.net/bug.php?id=61411

It seems there is a problem running both

PDO::ATTR_PERSISTENT => true

and

PDO::ATTR_EMULATE_PREPARES => true

Which you have in your PDO attributes/options array.

like image 137
spitfire Avatar answered Nov 11 '22 03:11

spitfire