What are some common pitfalls when using json_encode() with arrays from a MySQL database?

When using json_encode() with arrays from a MySQL database, a common pitfall is that special characters may not be properly encoded, leading to invalid JSON output. To solve this issue, you can use the JSON_UNESCAPED_UNICODE option in json_encode() to ensure that Unicode characters are not escaped.

// Fetch data from MySQL database
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Encode data as JSON with JSON_UNESCAPED_UNICODE option
$json_data = json_encode($data, JSON_UNESCAPED_UNICODE);

// Output JSON data
echo $json_data;