Json encode not able to fetch special characters from db in php

349 views Asked by At

I am learning to write ANDROID API in PHP to fetch data from the server.

The table in the server has a column "name" with value "Jàne" already present.

Below is the sample code:

<?php 
$reply= array();
if(isset($_POST['place'])){
// DB connection
$place= $mySQLiconn->real_escape_string($_POST['place']);
$search= $mySQLiconn->query("SELECT  name, id FROM info WHERE addr = '$place'");

if(!empty($search)){
    if($search->num_rows > 0) {         
        $reply["list"]= array();                        
        while($search = $search->fetch_array()){    
            $row = array();
            $row["id"] = $search["id"];
            $row["name"] = $search["name"];             
            array_push($reply["list"], $row);
        }           
        echo json_encode($reply);
    } else{
        // 
        $reply["msg"] = "Error";
        echo json_encode($reply);
    }   
} else{
    //Fetched failed
    $reply["msg"] = "Error";
    echo json_encode($reply);
}   
$mySQLiconn->close();

} else {
$reply["msg"] = "Error";
echo json_encode($reply); // point L
}
?>

If the result from the search result contains any special character, I get a 200 reply with a BLANK response body.

If the search result contains only A-Z, a-z, 0-9, comma, space ; then its working, I get a proper response body.

Please help me how to fetch the data with special character also.

EDIT

I want the same exact data in my response also, so that I can show it in the app UI.

There are other special characters also in the table like é , è , à, etc

Select command is working properly in my cpanel for special character also So, I think its not a database issue.

UPDATE:

The current code returns blank, but if i replace point L with echo var_dump($reply);

I get the response in following format:

array(1) { 
["list"]=> array(3) {
    [0]=> array(2) { 
        ["id"]=> string(2) "31" 
        ["name"]=> string(4) "Maze" 

    } 
    [1]=> array(2) { 
        ["id"]=> string(2) "35" 
        ["name"]=> string(4) "Jan�" 

    } 
    [2]=> array(2) { 
        ["id"]=> string(2) "39" 
        ["name"]=> string(7) "Puchong" 
    } 
}
}   
0

There are 0 answers