0

I have a prepared statement written on PHP that retrieves a string from MySQL, then i use json_encode to send the data to client, and it works perfect.

The problem happens when the string in MySQL contains “ .

Should i encode it differently? Or use some special flags?Or there are other solutions?

Thanks

hakre
  • 193,403
  • 52
  • 435
  • 836

1 Answers1

1

Should i encode it differently?

I'd say yes. Obviously the string you receive from the database is not UTF-8 encoded. And that's the problem, because json_encode­Docs needs UTF-8 encoded strings. If they are invalid, it will return NULL - because there was no valid data to encode.

You can verify this by checking for the last error with the json_last_error­Docs function.

So when you query data from your database, tell the database server that you expect UTF-8 encoded data by setting the database client encoding. Consult the documentation of the database client library you're using, it's documented there.

See as well json_encode() non utf-8 strings? which shows how you can re-encode the strings itself if you don't want to change the database client connection.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836