0

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.

1 Answer 1

1

json_encode() encodes multibyte unicode characters as\uXXXX. If you don't want that you can use the JSON_UNESCAPED_UNICODE option like this:

echo json_encode($output, JSON_UNESCAPED_UNICODE);

See the documentation on json_encode() and JSON constants

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, you answer has lead me to know a bit more abount JSON. The true problem was I tried to use JSON without knowing enough about it. I thought I could just work with the String produced from the php code echo json_encode(), after removing from it escape characters etc. From the link you posted I've learned instead that I had to decode the json string first. There where some additional trouble with the decoded string format (it had to be in the form {name:value}), but now the whole procedure works flawlessly. Thank you :) PS.: should I be the one to put the [SOLVED] tag in the title?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.