简体   繁体   English

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

[英]utf-8 character set not working in php file

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. iv在线查看大量信息,但没有解决方案。 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. 当我在数据库中运行带有字符汉语/汉语的插入查询时,它可以工作,所以我知道我的数据库已针对utf8进行了设置...这是我在PHP文件中所做的事情……这将是任何帮助。不胜感激。

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

You need to make sure your database is operating in UTF-8 mode. 您需要确保数据库以UTF-8模式运行。 You need to do this on the table itself. 您需要在表本身上执行此操作。 You said you INSERTED the string, but did you check to see if you can read it back out? 您说过您插入了字符串,但是是否检查了一下是否可以读出来? As noted in my comment below, you should be sure that you're connecting to MySQL with UTF-8 enabled. 正如我在下面的评论中所指出的那样,您应该确定要连接到启用了UTF-8的MySQL。 There is more information in the answer below: 在下面的答案中有更多信息:

How to make MySQL handle UTF-8 properly 如何使MySQL正确处理UTF-8

Additionally, be sure that the PHP file itself is being saved in UTF-8 format. 此外,请确保PHP文件本身以UTF-8格式保存。 Last but not least ensure the HTML page is correctly set to use utf-8: 最后但并非最不重要的一点是,确保将HTML页面正确设置为使用utf-8:

<meta charset="utf-8"> vs <meta http-equiv="Content-Type"> <meta charset =“ utf-8”>与<meta http-equiv =“ Content-Type”>

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_query

mysql_set_charset('utf8',$con);

// only for MySQLi //仅适用于MySQLi

$mysqli->set_charset("utf8")

also check out: http://tympanus.net/codrops/2009/08/31/solving-php-mysql-utf-8-issues/ 还要签出: 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. PHP过去并不是本地UTF-8友好的,您必须依靠下面的辅助功能。 I'm pretty sure all of the functions on the left have become UTF-8 friendly for a few years now. 我敢肯定,左侧的所有功能已经成为UTF-8友好型产品已有几年了。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM