doufeixuan8882 2016-10-08 05:21
浏览 39
已采纳

在sql中获取中文字母以在php中显示

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

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-10-08 05:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?