after setting all config file and runtime options for charset that i can find to utf-8, new mysqli connections made with php still has its charset set to latin1, which effectively means that i have to call $mysqli->set_charset('utf8')
each time i connect.
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error)
err_handle("mysql connect error({$mysqli->connect_errno}).");
if (!$mysqli->set_charset("utf8"))
err_handle("db error({$mysqli->errno}).");
i wonder if there is a permanent way of doing this?
similar problem was encountered in this post.
a "show variables like 'character_set%'
" query on the mysql server before calling $mysqli->set_charset('utf8')
shows:
(this part was ambiguous in previous revs)
character_set_client latin1
character_set_connection latin1
character_set_database utf8
character_set_filesystem binary
character_set_results latin1
character_set_server utf8
character_set_system utf8
the client, connection and results charset can only be changed to utf8 with $mysqli->set_charset('utf8')
at runtime. after that it shows:
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
i have
default_charset = "utf-8"
set in php.ini, and
[client]
default-character-set=utf8
...
[mysqld]
## This option is deprecated in favor of --character-set-server.
#default-character-set=utf8
set in my.cnf.
the default charset for my tables is also utf8.
seems like the "[client]" options only affect the cmd "mysql" tool and have nothing to do with php.
the return value of $mysqli->character_set_name()
is always latin1 no matter what i do, until $mysqli->set_charset('utf8')
is called.
i guess "latin1" is a mysql thing, since i cant recall anything else that defaults to "latin1" on my system.
^update: according to mysql manual 9.1.4, 9.1.5 and 5.1.3, character_set_client
should be provided by the client. i guess php doesn't provide it upon connection and mysql uses the fall-back charset latin1.
i'm running php 5.3 on debian wheezy with mysql 5.1.
any suggestion?
updated with info from comments:
i forgot to mention the skip-character-set-client-handshake
directive and why i was reluctant to use it.
upon first sight i thought ignoring the handshake might result in the situation that the client talks latin1 while the server talks utf8. how does the server convert the string from charset character_set_client
to character_set_server
without knowing the charset currently in use?
correct me if i'm wrong, plz. i will experiment with this setting later today to see if it works.
Updated with workaroud:
make sure everything works under utf-8 (or any preferable charset). then add the skip-character-set-client-handshake
line to my.cnf
.
this works for me so far. i experimented with some double-width utf-8 characters. both insert
and select
succeeded and displayed properly in the browser.
what skipping the handshake means is still unclear. and the mysql server now becomes uncapable of using any charset except utf-8, whick makes this workaround quite impractical since i simply cant apply this setting to all the servers that my website runs on.
so i'm not adopting this workaround. further comments and answers are much appreciated.