PHP/JSON/MYSQL: PHP output JSON Web Service "charset UTF-8" get multi languange data like hindi,franch etc

If we have to add another langauge data like franch,hindi,gujrati etc, we will have to change Collation as "utt8_general_ci".

Below are the detailed code and easy steps.

PHP code:

include_once("conif.php"); // include database config file
//$uid = isset($_GET['uid']) ? mysql_escape_string($_GET['uid']) : "";

$uid = 1;
mysql_query ("set character_set_results='utf8'");// set names mysql query
    if(!empty($uid)){
       $qur =  mysql_query("select name,address from gujrati_data where id ='$uid'");
       $result = array();
   
       while($r = mysql_fetch_array($qur)){
       extract($r);  
          // echo $r['name'];
           //echo $r['address'];
         
           $result[] = array("name" => $name,"email" => $address);
     
    }    
       $json = array("status" => 1, "info" => $result);
    }else{
     
       $json =  array("status" => 0, "msg" => "User ID is Not Define");
    }
    header('Content-Type: application/json; charset=utf-8'); // set this header in json
    @mysql_close($conn);
    //echo json_encode($json);
   
    echo (json_encode($json,JSON_UNESCAPED_UNICODE));

// add this JSON_UNESCAPED_UNICODE in json output

Below are the steps show what changes we have to made in our query

Step 1:

Change database field Structure.

open Table fileld Structure and change in collation set there "utf8-general_ci". Below is screenshot

Step 2:

set mysql_quey before fire query

mysql_query ("set character_set_results='utf8'"); 

Step 3:

header('Content-Type: application/json; charset=utf-8'); // set this header in json

If we want this in html then we can use

header ('Content-type: text/html; charset=utf-8'); ?> 

also you can use


in html use echo $r['name']; it get in multi langage

Step 4:

After end of json return json_encode your rensponse with JSON_UNESCAPED_UNICODE

like this

echo (json_encode($json,JSON_UNESCAPED_UNICODE));

Hope this post will helpful for you :)

No comments:

Post a Comment