user132638
user132638

Reputation: 69

Mysql php character set setting

At first we was using the default character set by the mysql that was latin1_swedish_ci. Then we had problem with other languages like russian etc. So we have now converted the tables into collation and the field into utf8_unicode_ci. We need some confirmation here should we stick with utf8_unicode_ci for the support all the possible languages or go for something else like utf8_bin?

Next issue for php files where we are using mysqli we have set this.

if (!mysqli_set_charset($link, "utf8")) {
    echo("Error loading character set utf8: ". mysqli_error($link));
  } 
  else {
    echo("Current character set: ". mysqli_character_set_name($link));
  }

Then for places where we use mysql only we did as below

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'"); 

The problem now we find this is like quite tedious and we are scared we might be miss. Is it possible to set this character setting in like a config file like we have one for our db connection?

Upvotes: 0

Views: 3311

Answers (1)

CodeAngry
CodeAngry

Reputation: 12995

Don't mix mysql_* with mysqli_* functions. You need to stay consistent! You use mysqli_ first and then you use mysql_. That won't work!

This is how I do it:

mysqli_set_charset($Handle, 'utf8'); // <- add this too
mysqli_query($Handle, "SET NAMES 'utf8';");
mysqli_query($Handle, "SET CHARACTER SET 'utf8';");
mysqli_query($Handle, "SET COLLATION_CONNECTION = 'utf8_unicode_ci';");
// might be a bit redundant but it's safe :) ... I think :)

Then make sure you provide proper UTF8 to it.

Upvotes: 4

Related Questions