How to use UTF8 in JSON with PHP?

Asked

Viewed 133 times

0

I am making a query in a sqlserver database with PHP/PDO then return a JSON to search in Angular JS.

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($result);
print $json;

However, the values it contains, for example:

string(17) "Iniciou Inscri��o"

In JSON it returns as null. My possible solutions, includes:

header('Content-Type: application/json; charset=utf-8');
json_encode($result, JSON_UNESCAPED_UNICODE)

None of them solved it. Someone has a solution ?

  • Please insert "inscrição" instead of "inscription". If it happens what I think I already give you a solution

  • I couldn’t understand it. I’m looking for it directly in the database.

  • Yes I know, I’ve had this problem too, it’s because it shouldn’t be set to utf-8

  • So, I change this directly in the database ? It’s not just that phrase. There are several. In the database you are searching right, but when I search directly in php, it brings with these questions.

  • Just one, to test. In that same word, change to what I told you above

  • It did not work, he returned me inscrição.

  • Looks like you really have to mysql_query("SET NAMES 'utf8'"); in its database, http://stackoverflow.com/questions/18009075/php-output-json-web-service-charset-utf-8-error

  • I am using PDO and this command does not work on SQL Server.

  • Usually I go through the record by adding the value in a new array, where the value deal with utf8_encode. This is because the database is ISO and the return of JSON and UTF8.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.