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?
There are two different character sets at issue:
character_set_client); andcharacter_set_results).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_setvariable 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With