2

i have a Persian data base that use collection utf8-general-ci but when i run below code i face with ???? instated of Persian words .

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "root", "", "testinsta");
$result = $conn->query("SELECT m_1, m_2, m_3 FROM codecode");
mysqli_query($conn, "SET NAMES utf8");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
  if ($outp != "") {$outp .= ",";}
  $outp .= '{"Name":"'  . $rs["m_1"] . '",';
  $outp .= '"City":"'   . $rs["m_2"]        . '",';
  $outp .= '"Country":"'. $rs["m_3"]     . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>

mysql to json with php problem

mysql to json with php problem

Eddie
  • 26,593
  • 6
  • 36
  • 58

1 Answers1

0

You can do like this,

$conn = new mysqli("localhost", "root", "", "testinsta");
$conn->set_charset("utf8");

and remove below line,

mysqli_query($conn, "SET NAMES utf8");

and now check.

Here is brief details about Whether to use “SET NAMES”.

Rahul
  • 18,271
  • 7
  • 41
  • 60