Joachim
Joachim

Reputation: 320

PDO equivalent of mysql_client_encoding()?

Is there any way in PDO to check the client encoding, like there was in mysql/mysqli with mysql_client_encoding();?

On PHP.net it states that one can set the charset with PDO::setAttribute(), e.g.:

$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");

But how does one get the current charset?

Upvotes: 3

Views: 1485

Answers (1)

eggyal
eggyal

Reputation: 125855

There are two different character sets at issue:

To ascertain the current value of these variables using PDO, you could fetch the results of the relevant SHOW VARIABLES statement; for example:

$qry = $db->query("SHOW VARIABLES LIKE 'character_set_client'");

The documentation for mysql_client_encoding() is somewhat ambiguous, as it states:

Retrieves the character_set variable from MySQL.

However, no such server system variable exists: so I'm not sure which it would return.

Finally, rather than setting a MYSQL_ATTR_INIT_COMMAND, you can specify your desired character set in the DSN (as mentioned in the manual):

$db = new PDO("mysql:dbname=$db;host=$host;charset=$charset", $user, $password);

Upvotes: 5

Related Questions