1

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" 
    } 
}
}   
OnePunchMan
  • 720
  • 15
  • 33
  • You need to escape special characters. [Take a look at this previous post](http://stackoverflow.com/questions/7552253/how-to-remove-special-characters-from-a-string) – Claus Dec 14 '16 at 14:02
  • @Claus I have tried that also, If I remove that, I will get the name as **Jne** in my response. I want the same exact data in my response "Jàne" – OnePunchMan Dec 14 '16 at 14:04
  • Do you have the mbstring module installed for PHP? – ryantxr Dec 14 '16 at 14:08
  • @ryantxr I am new in this field (server side, php...), could you please elaborate more, how can I know the details.. Currently, I am learning using cpanel hoisting from my godaddy account. – OnePunchMan Dec 14 '16 at 14:11
  • If you can access the command line, type `php -m`. It will list all the php modules. – ryantxr Dec 14 '16 at 14:18
  • @OnePunchMan Have you tried doing a select on your data base outside php? you could try to insert an escape character `J\àne`. This would let you know if it is a database issue or php issue – Claus Dec 14 '16 at 15:18
  • @Claus select command is working properly in my cpanel. It is not working in the json_encode in php – OnePunchMan Dec 14 '16 at 15:32
  • 1
    Does your `$mySQLiconn` is connected with UTF-8 charset ? json_encode() only works with UTF-8 special characters – HeavyNounours Dec 14 '16 at 15:35
  • 1
    @OnePunchMan Just to clarify are you saying "Jàne" is in your $reply variable but it disappears after you json_encode it? If not sure insert a `echo var_dump($reply);` So we can isolate between the query and the json_encode. Also your query looks a little suspect not that it would influence your outcome `$search= $mySQLiconn->query("SELECT name, id FROM info WHERE addr = '$place'");` Since you are inserting the variable `$place` as a parameter you need to change it to `addr = ".$place."` – Claus Dec 15 '16 at 15:23
  • @Claus Sorry for late reply. I am able to get a response from echo var_dump($reply); . But it is not in json format. How do I get the response in json format? Also, é is returned as � – OnePunchMan Dec 17 '16 at 11:17
  • @OnePunchMan **`[1]=> array(2) { ["id"]=> string(2) "35" ["name"]=> string(4) "Jan�" `** This tells us you do not have the information you thought in the database. So before you start looking at the JSON response, you need to look at how you are inserting/updating the data in your database. In your initial post you indicated the **a** had the accent mark. Are you now saying the **e** has the accent mark? Also `var_dump()` is just a diagnostic tool not to replace your JSON response. It merely tells you what is in your output array from your database. – Claus Dec 20 '16 at 12:47
  • @OnePunchMan actually you do have a 4 character response, but you need to look more closely at your `$mySQLiconn->query(...` as mentioned by HeavyNounours. Because your response from the server is not what you expect. So your problem so far is not with JSON. – Claus Dec 20 '16 at 12:54

0 Answers0