dongqi6964 2013-08-24 14:32
浏览 59
已采纳

utf-8字符集在php文件中不起作用

Im trying to insert 汉语/漢語 characters into my database but im only getting ????? when i do. Iv look at loads of information on line but no solution works. When i run an insert query in my db with the characters 汉语/漢語 it works, so I know my db is set-up for utf8...Its somthing im doing in my PHP file that's the problem...any help would be greatly appreciated.

<?php
include 'config.php';  
header('Content-Type:text/html; charset=UTF-8');
mysqli_query("SET NAMES utf8");
mysqli_set_charset('utf8');


// check for required fields
if (isset($_POST['name'])) {
$name = mysqli_real_escape_string($link, $_POST['name']);

$result = mysqli_query($link, "INSERT INTO scores(`id` , `name`)VALUES(NULL, '$name'");
  • 写回答

2条回答 默认 最新

  • duanjuebiao6730 2013-08-26 14:42
    关注

    Here's an old cheat sheet I rely on. Keep mind that some of this info is out of date

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

    // only for legacy MySQL_query

    mysql_set_charset('utf8',$con);
    

    // only for MySQLi

    $mysqli->set_charset("utf8")
    

    also check out: http://tympanus.net/codrops/2009/08/31/solving-php-mysql-utf-8-issues/

    PHP didn't use to be natively UTF-8 friendly, you had to rely on secondary functions like these below. I'm pretty sure all of the functions on the left have become UTF-8 friendly for a few years now.

    mail()                -> mb_send_mail()
    strlen()              -> mb_strlen()   
    strpos()              -> mb_strpos()
    strrpos()             -> mb_strrpos()
    substr()              -> mb_substr()
    strtolower()          -> mb_strtolower()
    strtoupper()          -> mb_strtoupper()
    substr_count()        -> mb_substr_count()
    htmlentities($var)    -> htmlentities($var, ENT_QUOTES, 'UTF-8')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路