Not able to fetch Unicode data in Hindi language from PHP and MySQL

房东的猫 提交于 2019-12-08 04:23:58

问题


I have a database (utf8_unicode_ci), tables (utf8_unicode_ci), and a mysql_query("SET CHARACTER SET utf8").

I have one more problem today. I have one table and in that there are 10,000 records which is stored in unicode format in the Hindi language. I am using Xampp 1.6.2 Version. When I fetch data on the local machine, it's showing properly; say for example "select * from unicode_table order by id".

But if i upload it on my live server, it's not displaying any record. I tried to figure it out and I found that if I write:

select * from unicode_table order by id limit 0, 40

A query with limit 0, 40 shows data, and if I give limit 0, 50 then it displays junk. So there is a problem in recordset/mysql query connection. Can any one help me out with this?

My live server is the latest update with PHP and MySQL and the server is maintained by one of the good hosting companies.

Thanks in advance.


回答1:


Things to watchout for : 1. make sure with your html or php file that would render the content, has either : i) <?php header('Content-Type: text/html; charset=utf-8'); ?> OR ii).

<?php $page_html = "<html>";

    $page_html .= "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=utf-8\">";

    $page_html .= "<body>";

    $page_html .= " process and display your content from the database here ";

$page_html .= "</body></html>";

    echo $page_html;
  1. Make sure you have the right collation as stated below. ( :) all the best )

So in my case, I had tried changing the collation from utf8mb4_unicode_ci for mysql and had to change it to uft8_general_ci.

Then pasted :

mysqli_set_charset( $con, 'utf8');

right before I did the SELECT command.

This is my code for reading from 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 chineese, russian or arabic or any international language from the database's table column holding such data.



来源:https://stackoverflow.com/questions/12581474/not-able-to-fetch-unicode-data-in-hindi-language-from-php-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!