Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO: Multiple commands in PDO::MYSQL_ATTR_INIT_COMMAND

Tags:

php

pdo

i'm using this PDO connection:

try{

$db=new PDO("
mysql:host=localhost;
dbname=...",
"dbu...",
"pass",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET lc_time_names='de_DE'"));
}
catch(PDOException $e){die("Error!");}

Now i would like to add another init command:

array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET lc_time_names='de_DE'",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
)

But it looks like that the utf8-one overwrites the first init command.

So i've tried this one but also without success:

array(
PDO::MYSQL_ATTR_INIT_COMMAND => array("SET lc_time_names='de_DE'","SET NAMES utf8")
)

Any idea how to send more than just one init command?

Thanks!

like image 944
Chama Avatar asked Feb 15 '16 17:02

Chama


1 Answers

The correct syntax is:

array
(
    PDO::MYSQL_ATTR_INIT_COMMAND
    => 
    "SET lc_time_names='de_DE',NAMES utf8"
)

So, NOT multiple arrays, but string with comma as separator and without repeating SET.


Edit: according with official documentation:

A SET statement can contain multiple variable assignments, separated by commas.

like image 61
fusion3k Avatar answered Oct 14 '22 02:10

fusion3k