6

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 so:

$mysqli->get_charset(); 

That doesn't work on $conn = new PDO();

Any help is appreciated. I'm fairly new to PDO and trying to jump over from mysqli.

P.s. i am aware how to set the character set, just want to test how to retrieve it.

Thanks to HD answering the question I made his method static, perhaps its helpful for someone (I used some prior to 5.4 notation):

 public static function getCharset(PDO $objPdo) { $objCharset = $objPdo->query("SELECT COLLATION('foo')"); $charset = $objCharset->fetch(PDO::FETCH_NUM); return $charset[0]; } 
5
  • 2
    Interesting, I've never thought about it. I don't think there is a way to get the character set using PDO. Commented Sep 30, 2014 at 15:27
  • @JayBlanchard Maybe OP means to use stackoverflow.com/a/4361485 - Not sure if this falls under as a duplicate though. Having a hard time understanding the question. Commented Sep 30, 2014 at 15:29
  • Hi Fred, its not a duplicate as far as I'm concerned. My question is not about setting the character set, but "how" to "retrieve" the characterset with PDO just like the get_charset function with mysqli Commented Sep 30, 2014 at 15:32
  • @rinserepeat Which is why I said I was not sure. Thanks for the added info. See the answer given below. Commented Sep 30, 2014 at 15:37
  • @rinserepeat do not include the answer inside your question. It's Ok If you provide an answer to your own question as a separate answer. Commented Feb 24, 2017 at 11:59

2 Answers 2

9

If we run the following MySQL query;

mysql> SELECT COLLATION('foo'); +-----------------------+ | COLLATION('foo') | +-----------------------+ | utf8_general_ci | +-----------------------+ 1 row in set 

So, we can use the following to get the charset

$objCharset = $objPdo->query("SELECT COLLATION('foo')"); echo $objCharset->fetch(PDO::FETCH_NUM)[0]; //Output: utf8_general_ci 

You can then go a step further, and use the following (with object injection).

<?php class foo { public function get_charset($objPdo) { $objCharset = $objPdo->query("SELECT COLLATION('foo')"); return $objCharset->fetch(PDO::FETCH_NUM)[0]; } } $objFoo = new foo(); $objPDO = new PDO(...); echo $objFoo->get_charset($objPDO); 
Sign up to request clarification or add additional context in comments.

5 Comments

Hi HD that seems to work with a small modifaction for me: $objCharset = $conn->query("SELECT COLLATION('foo')"); $charset = $objCharset->fetch(PDO::FETCH_NUM); echo $charset[0];
Ah okay! The shorthand that is used in this answer was introduced in PHP5.5 (I believe) :)
sidenote: actually dereference on functions are already on PHP 5.4 and above
@Ghost Thanks! I'll try remember that now
If you want the actual charset (not collation) use SELECT CHARSET(''); instead.
3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.