在sql中读取中文字母以显示在php中(fetch chinese letter in sql to display in php)

编程入门 行业动态 更新时间:2024-10-24 20:12:07
在sql中读取中文字母以显示在php中(fetch chinese letter in sql to display in php)

我正在尝试从mysql数据库中检索中文字母并在php上显示。 我已经尝试了互联网上的所有内容,但它仍然显示我“?” 在网站上。 这是我的代码,涉及到这个问题:

<html class="fsvs" lang="zh-Hans" > <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

在PHP部分:

$conn = mysqli_connect( $host, $user, $pass, $db ); if (!$conn) { die('Database connection failed.'); } //get gan char mysql_query("SET character_set_results=utf8", $conn); $query_gan = "SELECT * FROM gan WHERE gan_id = '$gan_id'"; $result_gan = mysqli_query($conn,$query_gan); $row_gan = mysqli_fetch_array($result_gan);

先谢谢了

I'm trying to retrieve chinese letters from mysql database and display on php. I have tried everything on the internet but it still shows me "?" on the website. Here is my code that related to this question:

<html class="fsvs" lang="zh-Hans" > <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In php part:

$conn = mysqli_connect( $host, $user, $pass, $db ); if (!$conn) { die('Database connection failed.'); } //get gan char mysql_query("SET character_set_results=utf8", $conn); $query_gan = "SELECT * FROM gan WHERE gan_id = '$gan_id'"; $result_gan = mysqli_query($conn,$query_gan); $row_gan = mysqli_fetch_array($result_gan);

thanks in advance :D

最满意答案

当我遇到类似的问题时,我首先在php中显示我的文本的编码

echo mb_detect_encoding ( $text );

它显示了来自我的查询的文本的编码。 这告诉我,当我的数据库中包含中文或俄文字符时,我从mysql_query获取ASCII。

我做的更改是在mysql_connect之后添加了以下内容

mysql_set_charset('UTF8');

我的数据库是utf8_unicode_ci整理,我可以看到中文字符。

所以,如果mb_detect_encoding现在为您提供了UTF8文本,那么您将能够显示中文字符。

下一步是确保你传递给浏览器的头具有正确的头...

header('Content-Type: text/html; charset=UTF-8');

把上面的代码放在php的顶部,以确保浏览器期待你的编码。

现在这应该是问题,但理想情况下你应该使用PDO或mysqli而不是mysql_connect方法。 在这种情况下,等效的程序样式命令是..

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test'); mysqli_set_charset($link, "utf8");

$ link等价于您与数据库的连接。

When I had a similar issue I firstly displayed the encoding of my text in php using

echo mb_detect_encoding ( $text );

It shows the encoding of the text coming from my query. This showed me that I was getting ASCII from mysql_query when Chinese or Russian characters were in my database.

The change I made was with the following addition after the mysql_connect

mysql_set_charset('UTF8');

My database is utf8_unicode_ci collation and I can see chinese characters.

So, if mb_detect_encoding is now giving you UTF8 for your text then you would be able to show chinese characters.

The next step is to make sure what you pass to the browser has the correct header...

header('Content-Type: text/html; charset=UTF-8');

Put the above at the top of your code in php to make sure the browser is expecting your encoding.

Now that should the question, however ideally you should be using PDO or mysqli rather than mysql_connect method. In this case the equivalent procedural style commands are..

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test'); mysqli_set_charset($link, "utf8");

Where $link is the equivalent to your connection to the database.

更多推荐

本文发布于:2023-08-04 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中文   字母   php   sql   letter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!