Displaying european chars and encoding JSON

Posted under » PHP » MySQL on 22 Oct 2018

While the UTF-8 character set will display special characters on the client, the server, on the other hand, may not be so accomodating and would print special characters such as à and è as � and �. To make sure your server will print them correctly, us the ISO-8859-1 charset:

¿php
    header('Content-Type: text/html; charset=ISO-8859-1');
?

The reason this works is that the client (browser) is being told, through the response header(), to expect an ISO-8859-1 text/html file. Then, once the browser begins to parse that given file into the DOM, the output will obey any rule but keep your ISO characters in tact.

However, when encoding to JSON, this is not enough. You need to set it on the mysqli level

mysqli_set_charset($dbhandle, 'utf8mb4');

$query = mysqli_query($dbhandle, 'SELECT author_name, title FROM publication_details LIMIT 100');
while ($row = mysqli_fetch_assoc($query)) {
        $data[] = $row;
}
$king = json_encode($data);
echo $king;

Note that utf8mb4 is more than utf8. However, old version of MySQL can only understand utf8.

The next step is to Decode JSON.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data