Damiano Barbati
Damiano Barbati

Reputation: 3496

PDO throwing error if MYSQL_ATTR_INIT_COMMAND is used

The PDO::MYSQL_ATTR_INIT_COMMAND seem to break down the query, try your own.

The following:

  $connection = new \PDO("mysql:host=localhost;dbname=php_orm_test", "dev", "dev",[
     PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8; SET CHARACTER SET UTF8; SET character_set_connection=UTF8; SET character_set_client=UTF8;"
  ]);
  $statement = $connection->prepare("describe users");
  $statement->execute();
  $statement->fetchAll(\PDO::FETCH_ASSOC);
  var_dump($statement->errorInfo());
  $statement->closeCursor();

Throws:

array(3) {
  [0]=>
  string(5) "00000"
  [1]=>
  int(2014)
  [2]=>
  string(269) "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."
}

If I remove the MYSQL_ATTR_INIT_COMMAND everything works fine instead... any idea?

Notice that not even this does work...

    PDO::MYSQL_ATTR_INIT_COMMAND => self::DRIVER_INIT,
    PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true

Upvotes: 2

Views: 5653

Answers (2)

Fabian Loibl
Fabian Loibl

Reputation: 51

You can at least use multiple SET statements delimited by commas, e.g. SET names utf8, @@session.sql_mode = '';

Upvotes: 5

bitWorking
bitWorking

Reputation: 12665

You are using PHP >= 5.3.6, so add charset to dsn and don't use PDO::MYSQL_ATTR_INIT_COMMAND. Further I think you can only execute 1 query with PDO::MYSQL_ATTR_INIT_COMMAND. That might be the problem.

mysql:host=localhost;dbname=php_orm_test;charset=utf8

Upvotes: 6

Related Questions