The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Not getting proper JSON

BangaruBabu's Avatar

BangaruBabu

07 Aug, 2015 12:49 PM

He llo All ,

Am very new to php , I am facing issue while rendering JSON data from mysql .

issue description
______________
In below Sample JSON we are getting special character("??????" ) in ArticleTitle . But that is not actual result . Actual data should be telugu regional language charecters . My ArticleTitle field is varchar datatype

JSON DATA
______________

{"status":"Success","ArticleTitles":[{"ArticleID":"865","ArticleTitle":"ANASUYA DEVI (1923-1985)"},{"ArticleID":"866","ArticleTitle":"?????? ??????"},{"ArticleID":"863","ArticleTitle":"???? ????????????? "},{"ArticleID":"862","ArticleTitle":"CONVEYING THE TRUTH WHICH SPEECH CANNOT SAY"},{"ArticleID":"861","ArticleTitle":"Jillellamuadi Amma"}]}

MyCode
__________

function getUsers() {
//$sql = "SELECT user_id,username,name,profile_pic FROM users ORDER BY user_id";
$sql = "select vid as ArticleID ,title as ArticleTitle from node where type='article' and status=1 order by created desc";

try {
$db = getDB();
$stmt = $db->query($sql);
$artilcetitle = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
//$encodedArray = array_map(utf8_encode, (string)$artilcetitle);
$result = array("status" => "Success" ,"ArticleTitles" => $artilcetitle);
echo json_encode($result);

} catch(PDOException $e) {
//error_log($e->getMessage(), 3, '/var/tmp/php.log');
//echo '{"error":{"text":'. $e->getMessage() .'}}';
$result = array("status" => "error", "message" => 'Exception: ' . $e->getMessage());
echo json_encode(utf8ize($result));
}
}

function utf8ize($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = utf8ize($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
}

  1. 1 Posted by Rob Allen on 07 Aug, 2015 02:25 PM

    Rob Allen's Avatar

    You have an encoding issue. Have a read of http://akrabat.com/utf8-php-and-mysql/ and see if it helps.

  2. 2 Posted by yoursbangarubab... on 08 Aug, 2015 01:40 PM

    yoursbangarubabu's Avatar

    Hello Allen, Thanks for your response

    You saved my day . With the help of the URL I made two changes in my code

    1) I passed following parameter in PDO connection object as per the URL you gave.

    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")

    2) In the above try {} code block I changed json_encode method as below

    echo json_encode($result,JSON_UNESCAPED_UNICODE);

    Thanks
    BangaruBabu

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac