Skip to content

Edoardo Vignati

– Always looking for something awesome –

Menu
  • About me
  • Studies
  • Publications
  • Skills
  • Portfolio
  • Projects
  • Career
  • Blog
  • Travels  
Menu

Coherent UTF-8 data manipulation from MySQL to JSON in PHP

Posted on 20/10/202224/10/2022 by Edoardo

This short guide may help you to quickly create a simple data flow from the query to the JSON response for example if you want to build an API endpoint. You can find here some details that can save you time and answer to the following question: “Why my accented chars and emojis are not displayed anymore?”.

This may happen if you upgrade MySQL and many chars are not working anymore in your web application. This problem could be easily solved editing the /etc/mysql/my.cnf (Ubuntu) file adding the following configuration rows to restore the UTF8 charset by default:

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
collation-server = utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4

Remember then to restart the MySQL daemon:

 $ sudo service mysqld restart

If you don’t have access to this file here is a specific focus on how to deal in particular with accented characters and other special chars.

Before to start, backup your tables/database. Safety first.

Then connect to PhpMyAdmin and set the collation of the involved tables to utf8mb4_unicode_ci (change also all columns collations).

Build your PHP script (version 8.1) in such a way:

<?php
	// Connection to db and charset
	$conn = get_connection();
	$conn->query("SET NAMES 'utf8'");
	$conn->query("SET CHARACTER SET 'utf8'");

	// Query execution
	$ssql = $conn->query("SELECT * FROM <mytable>");
	$data = $ssql->fetch_all(MYSQLI_ASSOC);
	$conn->close();

	// Headers for JSON/UTF-8 encoding
	$status = 200;
	header("HTTP/2 " . $status);
	header("Content-Type: application/json; charset=UTF-8");

	// Setup of the JSON response
	$response["status"] = $status;
	$status_message = "Something here";
	$response["status_message"] = $status_message;
	array_walk_recursive(
		$data,
		function (&$entry) {
			$entry = mb_convert_encoding($entry,"UTF-8");
		}
	);
	$response["data"] = $data;
	$json_response = json_encode($response);

	// Output response
	echo $json_response;

	// Function to get PDO connection
	function get_connection(){
		$servername = "localhost";
		$username = "<myusername>";
		$password = "<mypassword>";
		$db = "<mydb>";
		$conn = new mysqli($servername, $username, $password, $db);
		if ($conn->connect_errno)
		    exit();
		else
		    return $conn;
	}
?>

You can also specify the charset in the html code if needed:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Buy Me A Coffee


This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Edoardo on [SOLVED] XMLHttpRequest error in Flutter web is a CORS error06/01/2023
  2. Fabi on [SOLVED] XMLHttpRequest error in Flutter web is a CORS error20/12/2022
  3. Edoardo on How to install hplip on Ubuntu28/11/2022
  4. Alex K on How to install hplip on Ubuntu28/11/2022

© 2023 Edoardo Vignati | Powered by Minimalist Blog WordPress Theme