0

I have simple function that do this:

    $stmt = $mysqli->prepare("SELECT id, firstName, lastName, birthDate, address, phoneNumber, housingSituation1 FROM client WHERE id = ?");

    $param = 2;
    $stmt->bind_param("i", $param);
    $stmt->execute();

    $obj = $stmt->get_result()->fetch_object();

    $stmt->close();
    $mysqli->close();

    echo json_encode($obj);

This worked good until at one point i noticed its not returning anything but i get status 200 from that function, when i started debugging i seen that i have client with lastName: Müller, so after i repleaced ü with u that script started working again. Then i googled a bit and found that i can use something like this echo json_encode($obj, JSON_UNESCAPED_UNICODE);but its not working for me. Anyone knows how i can solve this issue?

Sahbaz
  • 1,242
  • 4
  • 17
  • 39
  • Have you tried the solution provided here: http://stackoverflow.com/questions/13602075/problems-with-german-umlauts-in-php-json-encode – J. Doe Feb 25 '17 at 10:18
  • 1
    http://stackoverflow.com/a/41692298/6369494 check this for PDO. – Tejas Mehta Feb 25 '17 at 10:22
  • I faced same problem with Arabic language. – Tejas Mehta Feb 25 '17 at 10:22
  • I seen that, but as you can see i;m using get_result()->fetch_object() feature of mysqlnd driver, and since my client table have ~70 attributes i dont want to bind every attribute one by one, do you know how i can apply solution from there to mine example ? – Sahbaz Feb 25 '17 at 10:23
  • 1
    You have 2 options either configure your database with UTF-8 or you write the code while connection of database. – Tejas Mehta Feb 25 '17 at 10:27
  • You will get result but cant pass to the operative functions – Tejas Mehta Feb 25 '17 at 10:28
  • I solved my issue thanks to links that you guys provided, i posted answer and it works great! @TejasMehta how to properly set database with UTF-8, i already on my every table have this ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE utf8_general_ci; – Sahbaz Feb 25 '17 at 10:30

1 Answers1

0

I solved issue like this, on my dbConnect php file i added mysqli_set_charset, and my dbConnect.php looks like this. I hope this helps someone.

$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '';
$DB_NAME = 'local_db';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
mysqli_set_charset($mysqli, 'utf8'); 
Sahbaz
  • 1,242
  • 4
  • 17
  • 39