Search code examples
phppdo

PDO: Multiple commands in PDO::MYSQL_ATTR_INIT_COMMAND


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!


Solution

  • PDO doesn't support multiple PDO::MYSQL_ATTR_INIT_COMMAND options. However, in case you need to execute multiple SET commands, you can use a workaround:

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

    From MySQL documentation:

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

    Note that regarding the SET NAMES command, it is the recommended to use the charset DSN option instead.