0

please how can I print Json with php I am using below code for English language everything is ok but with another languages output are unknowns character like ??????? ??????? ???? ?? - ????? - ?????? -??? ? how can print them like they are in database characters in phpMyAdmin I am using utf8_bin and this is my php code

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "clothesapp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$NumSub = 500;
$start = (int)$_GET['limit'];
$sql = "SELECT * FROM clothesapi  LIMIT $start , $NumSub";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

    $All_storys = array();
    while($row = $result->fetch_assoc()) {
        $All_storys[] = $row;
    }
} else {
    echo "0 results";
}
    $json_re=array();
    array_push($json_re,array("All_storys"=>$All_storys));
    echo json_encode($json_re);

$conn->close();
?>

if you have another code can do this please that can help me a lot

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Show us an example of the input data, and also the output JSON which you're seeing, please – ADyson Nov 04 '18 at 22:33
  • they appear like this . [ { "id": "11", "story_name": "??????", "img_link": "WELCOME ?????? ????? \r\n", "details": "???????? " } ] – Obeda Al Ale Nov 05 '18 at 19:32
  • and what about the input, as requested? – ADyson Nov 05 '18 at 19:42
  • btw a couple of side points: 1) You shouldn't echo "0 results" when the result set is empty. the client will be expecting JSON, and won't be able to parse this. Instead, just return a (json-encoded) empty array. Then leave it to the client to detect the number of rows, if it's important to it. – ADyson Nov 05 '18 at 19:59
  • 2) Never get your web app to login to the database as root. Root can do whatever it likes, so if there's an undetected vulnerability in your PHP code then this just leaves your database an open book for hackers. Instead create a separate user account specifically for this application which has only the permissions it actually _needs_ in order to work properly. Don't even use the root account as a shortcut during development or testing, because you need to test your account permissions as well - otherwise when you go live you might have unexpected errors relating to the user account setup – ADyson Nov 05 '18 at 20:01
  • Also, one suggestion, before you output the JSON, set the content type header: `header("Content-type: application/json; charset=utf-8");` and see if that makes any difference to how the output is viewed. And also, where are you actually seeing this output? In a web page? In a network tool? In PostMan or something? Maybe also try in some different client in case it makes any difference. And a separate question - what happens if you just echo the data without encoding as JSON? how does it look then? we should rule out the json encoding process as the culprit, if we can. – ADyson Nov 05 '18 at 20:16
  • Lastly, read this article: https://stackoverflow.com/questions/279170/utf-8-all-the-way-through and make sure you're following the recommendations. In particular I was thinking you should check you're connecting to the database using the right character set – ADyson Nov 05 '18 at 20:18
  • BTW I think utf8_bin is actualy a _collation_, and not a _character set_. – ADyson Nov 05 '18 at 20:25
  • 1
    See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Nov 06 '18 at 02:58

2 Answers2

0

finally I found the answer I should to put this line under connection with database

$conn = new mysqli($servername, $username, $password, $dbname);
       $conn->set_charset("utf8");
-1

1.Data in json should be utf8

2.Try this javascript https://mothereff.in/utf-8 when displaying.

3.If need further processing in php

JsonUtf8Decode(&$v, $k) { $v = utf8_decode($v); }array_walk_recursive($json_re, "JsonUtf8Decode");

This is from PHP manual.

afifio
  • 1
  • 1