android - Php modifies values retrieved from database (codification and special characters issues)
Get the solution ↓↓↓Although I've found some answers to questions similar to this one, I'm still not able to solve the following problem: I have a database using utf8 codification, created my means of
CREATE TABLE report (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
description TEXT,
image MEDIUMTEXT,
[...]
) DEFAULT CHARACTER SET utf8
I've successfully uploaded some entry from my android application. In particular, the image is codified by a string (the coding/undecoding works, so I'll not go back on this); however, when my android application contacts the server (executing a php script) for retrieving description and image, in "description" there is a problem with special characters (like 'ГЁ'), while the String codifying the image returned to the android application is filled with escape character ('\') in front of every special character (e.g. '/'), so the returned String is different from the sent one and it is not possible to retrieve the image from the corrupted String.
The query I'm going to execute (here the 'id' field is fixed for debugging purpose) is the following, and on phpMyAdmin it works fine
SELECT description, image FROM report WHERE id=8
I suppose the problem lies within the php script, which is the following (I'll paste only the relevant part)
<?php
header('Content-Type: application/json');
$action = $_POST['action'];
try {
$pdo = new PDO('mysql:host=activecitizen.altervista.org;dbname=my_activecitizen', 'activecitizen', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e) {
[...]
}
switch($action){
[...]
break;
case "get_report_details":
$report_id = $_POST['report_id'];
$report_id_int = (int)$report_id;
try{
$sql = 'SELECT description, image FROM report WHERE id = '.$report_id_int;
$result = $pdo->query($sql);
$row = $result->fetch();
$output .= $row['description'];
$output .= "~";
$output .= $row['image'];
echo json_encode($output);
exit();
}
catch (PDOException $e){
[...]
}
break;
}
?>
I thought the $pdo->exec('SET NAMES "utf8"'); line was enough in order to solve the codification problem, but it doesn't seem to be the case.
This is a preview of the string returned to the android application:
"qui c'\u00e8 qualcuno che non ha voglia di rifarsi il letto!~iVBORw0KGgoAAAANSUhEUgAAAUwAAAC6CAIAAABKsOO4AAAAA3NCSVQICAjb4U\/gAA[...]"
Here you can see that "c'\u00e8" is returned insted of "c'ГЁ" and iVBORw0KGgoAAAANSUhEUgAAAUwAAAC6CAIAAABKsOO4AAAAA3NCSVQICAjb4U/gAA instead of iVBORw0KGgoAAAANSUhEUgAAAUwAAAC6CAIAAABKsOO4AAAAA3NCSVQICAjb4U/gAA as (the first little piece of the string) returned by the query performed on phpmyadmin.
I'm sorry for the long question, and thank you in advance to anyone who is willing to help.
Answer
Solution:
json_encode()
encodes multibyte unicode characters as\uXXXX
. If you don't want that you can use theJSON_UNESCAPED_UNICODE
option like this:
echo json_encode($output, JSON_UNESCAPED_UNICODE);
See the documentation on json_encode() and JSON constants
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illuminate\http\exceptions\posttoolargeexception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.