Why can't PDO :: ATTR_EMULATE_PREPARES be disabled?

I am using PHP 5.2.9 and Apache 2.2.11 and mysql 5.1.32

Why can't I disable PDO :: ATTR_EMULATE_PREPARES?

Below is the code:

<?php try{ $conn = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS, array(PDO::ATTR_EMULATE_PREPARES => false)); } catch(PDOException $pe){ die('Connection error : ' .$pe->getMessage()); } $st = $conn->prepare('abc'); echo "emulate : " . $st->getAttribute(PDO::ATTR_EMULATE_PREPARES); ?> 

the output is "emulate: 1". I also tried the code:

 $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); 

but the output remains the same value of "1". The output should be 0 if the value is false right? But why is exit 1? How to disable ATTR_EMULATE_PREPARES?

+4
source share
1 answer

your code works for me on PHP 5.3.6 and mysqld 5.1.58 (it returns false and really uses cooks), try updating PHP to> = 5.3 to make sure this is a version problem (if so, then the update is probably , your only solution).

Despite the fact that even if you manage to set this flag, this does not mean that the PDO will use the prepared statement, if you want to check whether the PDO really uses cooks (and you can use wireshark ) you can write a simple script that prepared the request :

 <?php $pdo = new PDO(..., array(ATTR::PDO_EMULATE_PREPARES => false)); $stmt = $pdo->prepare('SELECT :param'); $stmt->bindValue(':param', 5); $stmt->execute(); 

Fool the transmission until you find the query "SELECT: param" - if: param has been replaced by a question mark, then PDO uses cook. If it has been replaced by "5", then PDO emulates the finished ones.

+3
source

Source: https://habr.com/ru/post/1388023/


All Articles