1

I am trying to get data in JSON format. In database, 'employee' table contains data in cyrillic

<?php
$mysqli = new mysqli("localhost","user","password","db");
    $myArray = array();
    if ($result = $mysqli->query("SELECT * FROM employee")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }
        echo json_encode($myArray);
    }

    $result->close();
    $mysqli->close();
?>

Result contains NULL instead of values in cyrillic.

[
{
id: "1",
lastname: null,
firstname: null,
middle: null,
occupation: null,
dob: "1991-01-01",
mobile: "+99999 9999999",
home: "+77777 7777777",
email: "testmail@mail.ru"
}
]

Output of myArray:

a:2:{i:0;O:8:"stdClass":9:{s:2:"id";s:1:"1";s:8:"lastname";s:7:"Азизов";s:9:"firstname";s:6:"Азиз";s:6:"middle";s:14:"Азизович";s:10:"occupation";s:19:"Android разработчик";s:3:"dob";s:10:"1991-01-01";s:6:"mobile";s:13:"+99999999999";s:4:"home";s:13:"+777777777777";s:5:"email";s:22:"testmail@mail.ru";}}

How to solve this problem?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

1 Answers1

0

I have just added $mysqli->set_charset("utf8");This line of code encodes data into UTF-8 format. Why it was not working? Because json_encode needs data in UTF-8

<?php
$mysqli = new mysqli("localhost","user","password","db");
$mysqli->set_charset("utf8");
    $myArray = array();
    if ($result = $mysqli->query("SELECT * FROM employee")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }

        echo json_encode($myArray);
        //echo serialize($myArray);
    }

    $result->close();
    $mysqli->close();
?>
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109