0

My problem is that how my query result unreadable for humans. I get this result in browser:

ID: 5 Reg.date: 2016-02-29 18:57:52 C�si

And the name should be 'Cósi'. The php is in UTF-8, the database is in utf8-hungarian-ci. So I do the query and after it I put the results into a $user array, and I echo the first 3 item like: echo "ID: " . $user["userID"] . "Reg.date: " . $user["regdate"] . $user["name"]; I tried header('Content-type: text/html; charset=UTF-8'); but it neither works. I have a innoDB phpmyadmin database, but the server is my father's computer. A xampp, should I search the problem there? Here is the all php:

$con = mysqli_connect("127.0.0.1", "kxxx", "csxxx", "bxxx");
$password = "asd";
$username = "carrie@gmail.com";
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $reg_date, $name, $email, $password, $phonenumber);
$user = array();

while(mysqli_stmt_fetch($statement)){
    $user["userID"] = $userID;
    $user["regdate"] = $reg_date;
    $user["name"] = $name; / which should be "Cósi"
    $user["email"] = $email;
    $user["password"] = $password;
    $user["phonenumber"] = $phonenumber;
}
echo "ID: " . $user["userID"] . "Reg. dátum: " . $user["regdate"] . $user["name"];

echo json_encode($user, JSON_UNESCAPED_UNICODE);

The json_encode is disappears, if empty [] should displayed, but that's not, only the first 3 item of the array.

So I want the C�si to be Cósi. What should I do? I tried to changed meta tag to charset='utf-8', mysql_query("SET NAMES 'utf8'") and the header thing what I mentioned above, all the columns are utf8-hungarian-ci, all the tables and the database is too.... So maybe the server configurations are bed?

When I insert into the database over a php, in the database the 'Cósi' is displays well, everything saved in the database right.

Thank you guys, I resolved it wtih:

mysqli_set_charset($con, 'utf8mb4');
  • What is the output of `mysqli_character_set_name($con)` ? Because connexions have encoding too. If not correct, see the function [`mysqli_set_charset`](http://php.net/manual/en/mysqli.set-charset.php) – Zimmi Mar 15 '16 at 18:05
  • @Zimmi For this: `if (!mysqli_set_charset($con, "utf8")) { printf("Error loading character set utf8: %s\n", mysqli_error($con)); exit(); } else { printf("Current character set: %s\n", mysqli_character_set_name($con)); }` The result: `Current character set: utf8` – Caroline Koosy Mar 18 '16 at 10:42

2 Answers2

0

You need the proper html tags:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Your Page Title</title>
</head>

<body>

YOUR CONTENT HERE

</body>
</html>
WheatBeak
  • 1,036
  • 6
  • 12
  • Unfortunately couse of the json I use only php, becouse where the json I use the HTML appears and the app crashes :S – Caroline Koosy Mar 15 '16 at 19:22
  • Can you view the source of your output in your browser and add it to your answer? – WheatBeak Mar 15 '16 at 19:36
  • The source in browser is only this: `ID: 5Reg. dátum: 2016-02-29 18:57:52C�si` There aren't any HTML or anything becouse where I user the json in the end of php, the app read all the things like , and so on, so I need only the pure php – Caroline Koosy Mar 16 '16 at 08:16
0

Check mb_convert_encoding in php.

$str = mb_convert_encoding($str, "UTF-8", "utf8-hungarian-ci");
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • 'utf8-hungarian-ci' is not a a type of character encoding, it is a collation. The third argument is supposed to be the character encoding of the source string. This will throw an error. – WheatBeak Mar 15 '16 at 18:30
  • If i change utf8-hungarian-ci to uft8mb4-bin or utf8-bin nothing changes =( – Caroline Koosy Mar 15 '16 at 19:25