How to take unicode characters from the mysql data base in PHP
html, mysql, php, unicode
Solution
I feel something goes wrong when I take values from the database. But i couldn't found the error. I have tried following codes also.
You need to make sure your entire chain from the connection, to the database, to the tables is all UTF8 clean. I have a detailed answer to a similar question here.
But in your case, check the actual MySQL server `my.cnf` file. The following would set the whole chain to UTF-8:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
More immediately, look at your code:
mysql_query("set collation_connection='utf8_sinhala_ci'");
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");
In one line you are calling `mysql_query` and the next you are calling `mysqli_query`. But these are conflicting methods that should not be used together. It should simply be `mysqli_query` like so:
mysqli_query($this->connecDB, "SET collation_connection='utf8_sinhala_ci'");
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");
Note how I am setting `mysqli_query($this->connecDB,…` before your `SET collation_connection='utf8_sinhala_ci'`. That will send the query on the same connection you are using for `$result`. Or perhaps you can try this instead:
mysqli_query($this->connecDB, "SET NAMES 'utf8'");
$result = mysqli_query($this->connecDB, "SELECT * FROM pageContent");
Problem
I am developing a local website which gives information in local language. In my MySQL database , there is a table called pageContent and it has a column called "text". that "text" column type is text and collation is utf8_sinhala_ci In MySQL it displays the fonts normally: But when I take it in to PHP page and echo the value, then it shows question marks like `??????????`. But if I hard code something and echo, then it displays normally in PHP page also. I feel something goes wrong when I take values from the database. But I couldn’t found the error. I have tried following codes also. ``` mysql_query ("set collation_connection='utf8_sinhala_ci'"); $result = mysqli_query($this->connecDB,"SELECT * FROM pageContent"); ``` But that doesn’t work.