如何从PHP中的mysql数据库中获取unicode字符 [英] How to take unicode characters from the mysql data base in PHP

查看:122
本文介绍了如何从PHP中的mysql数据库中获取unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个以本地语言提供信息的本地网站。在我的MySQL数据库中,有一个名为pageContent的表,它有一个名为text的列。 文本列的类型是文本和整理是utf8_sinhala_ci




在MySQL中,它通常显示字体:




但是当我将它带入PHP页面并回显值,然后显示问号如 ??????????




但是,如果我硬编码的东西和回声,那么它也正常显示在PHP页面。




我从数据库中获取值时感觉出了问题。但是我找不到这个错误。
我也试过以下代码。

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

但这并不奏效。

解决方案


当我从数据库中获取值时,感觉出错了。但
我找不到错误。我还尝试了以下代码。


您需要确保从连接,数据库到表格的整个链是所有UTF8清洁。我在这里有一个类似的问题有详细的答案 。

但在你的情况下,请检查实际的MySQL服务器 my.cnf 文件。以下将整个链设置为UTF-8:

  [客户端] 
default-character-set = utf8

$ mysql
default-character-set = utf8
$ b $ [mysqld]
collat​​ion-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8

更多立即看看你的代码:

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

在一行中,您调用 mysql_query 和接下来你要调用 mysqli_query 。但是这些是不应该一起使用的相互矛盾的方法。它应该只是 mysqli_query 就像这样:

  mysqli_query($ this- > connecDB,SET collat​​ion_connection ='utf8_sinhala_ci'); 
$ result = mysqli_query($ this-> connecDB,SELECT * FROM pageContent);

注意我如何设置 mysqli_query($ this-> connecDB,... 在你的 SET collat​​ion_connection ='utf8_sinhala_ci'之前。这将在你用于 $ result的连接上发送查询。或者你可以试试这个:

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


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.

解决方案

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"); 

这篇关于如何从PHP中的mysql数据库中获取unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆