From the MySQL’s C API function mysql_real_escape_string
description:
If you need to change the character set of the connection, you should use the
mysql_set_character_set()
function rather than executing aSET NAMES
(orSET CHARACTER SET
) statement.mysql_set_character_set()
works likeSET NAMES
but also affects the character set used bymysql_real_escape_string()
, whichSET NAMES
does not.
So don’t use SET NAMES
/SET CHARACTER SET
but PHP’s mysql_set_charset
to change the encoding as that is the counterpart to MySQL’s mysql_set_character_set
(see source code of /ext/mysql/php_mysql.c).