GET charset with mysql PDO

后端 未结 2 1345
走了就别回头了
走了就别回头了 2021-01-03 02:14

Is there a way of retrieving the current character set with PDO? I used to have a little test with Mysqli to check if the forced character set was set by retrieving it like

2条回答
  •  一整个雨季
    2021-01-03 02:49

    If you want the actual charset (not collation) you can do this:

    $charset = $pdo->query("SELECT CHARSET('')")->fetchColumn();
    if($charset === 'utf8mb4') {
        $charset = 'utf8';
    }
    

    I put the utf8mb4 check in there because it's not compatible with PHP's mb_ functions.

提交回复
热议问题