How to transfer Unicode characters from mysql database to PHP

I am developing a local website that provides local language information. My MySQL database has a table called pageContent, and it has a column called "text". this type of text is "text" - text, and - utf8_sinhala_ci

enter image description here

In MySQL, it usually displays fonts:

enter image description here

But when I take it to the PHP page and the echo value, then it shows question marks such as ??????????.

enter image description here

But if I worked something and the echo code, it also appears normally on the PHP page.

enter image description here

I feel that something is wrong when I take values ​​from the database. But I could not find the error. I also tried the following codes.

mysql_query ("set collation_connection='utf8_sinhala_ci'"); 
  $result =  mysqli_query($this->connecDB,"SELECT * FROM pageContent");  

But that does not work.

+4
3

, - , . . .

, , , - UTF8 . .

MySQL MySQL my.cnf. 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

, :

mysql_query("set collation_connection='utf8_sinhala_ci'"); 
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");  

mysql_query, mysqli_query. , . mysqli_query :

mysqli_query($this->connecDB, "SET collation_connection='utf8_sinhala_ci'"); 
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");

, mysqli_query($this->connecDB,… SET collation_connection='utf8_sinhala_ci'. , $result. , , :

mysqli_query($this->connecDB, "SET NAMES 'utf8'");
$result =  mysqli_query($this->connecDB, "SELECT * FROM pageContent"); 
+3

, PHP , (UTF8). .

, utf8_decode().

+1

, utf8mb4_unicode_ci mysql uft8_general_ci.

:

mysqli_set_charset( $con, 'utf8');

SELECT.

db:

/*

$DB_SERVER="db_server_name";
$DB_USER_READER="root";
$DB_PASS_READER="passw*rd";
$DB_NAME="db_name";
$DB_PORT="port number";

$SELECT_WHAT="`name_of_column_as_in_your_table`";
$WHICH_TBL="`table_name`";
$ON_WHAT_CONDITION="`id`='7'";

*/


$con = mysqli_connect($DB_SERVER, $DB_USER_READER, $DB_PASS_READER, $DB_NAME, $DB_PORT);//this is the unique connection for the selection

    mysqli_set_charset( $con, 'utf8');


        $slct_stmnt = "SELECT ".$SELECT_WHAT." FROM ".$WHICH_TBL." WHERE ".$ON_WHAT_CONDITION;

    $slct_query = mysqli_query($con, $slct_stmnt);

        if ($slct_query==true) {
//Do your stuff here . . . 
}

And it worked like a charm. All the best. The above code can work with reading Chinese, Russian or Arabic or any international language from the column of the mysql database table containing such data.

0
source

Source: https://habr.com/ru/post/1540054/


All Articles