Why is this PHP call to json_encode silently failing – inability to handle single quotes?

You need to set the connection encoding before executing queries. How this is done depends on the API you are using to connect:

  • call mysql_set_charset("utf8") if you use the old, deprecated API.
  • call mysqli_set_charset("utf8") if you use mysqli
  • add the charset parameter to the connection string if you use PDO and PHP >= 5.3.6. In earlier versions you need to execute SET NAMES utf8.

When you obtain data from MySQL any text will be encoded in “client encoding”, which is likely windows-1252 if you don’t configure it otherwise. The character that is causing your problem is the “curly quote”, seen as 92 in the hex dump, which confirms that the mysql client is encoding text in windows-1252.

Another thing you might consider is pass all text through utf8_encode, but in this case it wouldn’t produce the correct result. PHP’s utf8_encode converts iso-8859-1-encoded text. In this encoding \x92 is a non-printable control character, which would be converted into a non-printable control character in utf-8. You could use str_replace("\x92", "'", $input) to fix the problem for this particular character, but if there’s any chance there will be any other non-ascii characters in the database you’ll want to have the client use UTF-8.

Leave a Comment