0

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?

user132638
  • 69
  • 6
  • 13

1 Answers1

4

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.

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • @claudarian no I am not mixing both of them where I need transaction then I use mysqli and where I just select only then I mysql.So I stay consistent just that I do not know where I am doing things correctly. So which utf8 to be used it utf8_unicode_ci sufficient to represent all the characters? – user132638 Oct 27 '12 at 16:04
  • is the any way where I set it to be global value e.g. in a config file? Thank you. – user132638 Oct 27 '12 at 16:05
  • @user132638 Don't know of any, don't really care. I like to setup stuff myself and not rely on configs that may or may not be in place. Consider using mysqli_ only. It's easy to move to the new interface. mysql_* is discontinued... so if you're just starting out, steer clear of it. You are, as I understand, using two connections at the same time... which is not really efficient. – CodeAngry Oct 27 '12 at 16:41
  • @claudarian so well I guess there is no choice then I got to manually got and add every where my query are run or used right. Thank you. $ handle in my case is the database link right? – user132638 Oct 27 '12 at 17:13
  • @user132638 NO! You add only once after the connection is established. This change is session persistent. You connect with mysqli_connect(...) (or mysqli(...)) and call the charset specifiers only once, just after connection is established. And you're set for all next queries done on that connection. – CodeAngry Oct 27 '12 at 17:21
  • thank you I will do it so best to go with mysqli then thank you any specific version of it you would like to recommend or just go according the mysql db? – user132638 Oct 28 '12 at 08:15
  • @user132638 The newer your MySQL, the better. 5.5 is now. – CodeAngry Oct 28 '12 at 11:27