2

I am android developer and using PHP just for getting data from MySQL (so my PHP is very poor) and it always works but I come across that I can't get data with non-english letters

I have written that kind of question How to put in JSON Object non-english letters?

but in that question I thought that problem was in json encoding, but it was not problem in json.(but now I know how to solve problem with json) so I need just to get non-english text data from MySQL

I thought that problem was in database so I changed everything to utf-8 , checked twice, made the same query in database and get expected result, so problem is in php code. then I researched and thought that maybe my php didn't maintain utf-8, and that line <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> to the top then create a simple String variable that consist non English letters and echo that variable, and it has showed me what I expect, so I think php support utf-8

so I think the only problem is in the part with mysqli_query

that is my code :

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
header("Content-type: text/html; charset=utf-8");
$con =  mysqli_connect(...);
$sql = "SELECT * FROM news WHERE status = 'actual' ORDER BY id DESC";

$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - TITLE:  " . $row["title"]. " -TEXT:  " . $row["text"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($con);
?>

it is my data base datas: datas and structure: structure

for all database

    MySQL charset: UTF-8 Unicode (utf8)
    MySQL connection collation: utf8_unicode_520_ci

I expected to get the same datas as in title and text columns (you can see datas above)

but I get:

id: 4 - TITLE: ???????? -TEXT: ????????? ??????? ?????, ? ??? ????????? ?? ?????????
id: 3 - TITLE: A little bit... -TEXT: Just some more patience! And you will get this wonderfull application!
id: 1 - TITLE: �p�???�? -TEXT: Just wait a little bit!  

I tried to write with all details of my problem, so help me with code, what I should add/modify (in which line?) to get expected result. Thank you all in advance!

SOLUTION HAS BEEN FOUND

I should add $con->set_charset("utf8");

before declaring $sql

  • 1
    Does `$con` have the character set on it? https://stackoverflow.com/questions/10829816/set-character-set-using-mysqli – user3783243 Jan 31 '19 at 17:21
  • 1
    Show the details of your connection to MySQL. You need to specify the charset when making the connection. – Dave Jan 31 '19 at 17:25
  • @user3783243 excuse me, I got that you mean that I should use `$mysqli->set_charset("utf8");` , but sorry my php knowledge is poor, where should I write it? after which line? – Java Rzayev Jan 31 '19 at 17:43
  • Your script is sending the meta tag before it attempts to send a header. You can't send headers after you've output text to the browser. This should probably be generating errors. – S. Imp Jan 31 '19 at 17:46
  • @user3783243 THANKS YOU ! I have found solution thanks to you, I should add `$con->set_charset("utf8");` and it works! – Java Rzayev Jan 31 '19 at 17:48

0 Answers0