简体   繁体   English

在mysql db中存储中文字符不起作用

[英]storing chinese character in mysql db doesnt work

i am trying to store Chinese character in mysql database,but it store some messy character. 我正在尝试将中文字符存储在mysql数据库中,但是它存储了一些凌乱的字符。 Please help.. here is my code- 请帮助..这是我的代码-

<?php header("Content-Type: text/html; charset=UTF-8");
if(isset($_POST['submit']))
{
$conn=mysql_connect("localhost","root","") or die("CONNECTION FAILED"); 
 $db=mysql_select_db("mytest",$conn) or die("DATABASE DOES NOT EXISTS");
 mysql_query("SET character_set_client=utf8", $conn);
  mysql_query("SET character_set_connection=utf8", $conn);
mysql_query("INSERT INTO tbl_test(id,name)
VALUES (null, '".$_POST['fname']."')") or die(mysql_error());
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="" method="post">
Name: <input type="text" name="fname" />
<input type="submit" name="submit" />
</form> 
</body>
</html>

welcome to Charset Hell. 欢迎来到Charset Hell。

In PHP, to use a certain character set, absolutely everything has to be in the same character set. 在PHP中,要使用某个字符集,绝对所有内容都必须使用相同的字符集。 If you're intending to use UTF8, then everything needs to be UTF8. 如果您打算使用UTF8,则所有内容都必须为UTF8。 The database needs to be using UTF8 as its charset, the connection between the script and the DB needs to be UTF8 (it looks like you've taken care of this already), the PHP script itself needs to be saved as a UTF8 file (without a BOM, because having a BOM will just cause you more trouble later), and the browser has to be viewing the page generated by the script in UTF8 (you should use the header() command to send a content-type header that specifies this). 数据库需要使用UTF8作为其字符集,脚本与数据库之间的连接必须为UTF8(看起来您已经做好了这件事),PHP脚本本身需要另存为UTF8文件(没有BOM表,因为拥有BOM表只会在以后给您带来更多麻烦),并且浏览器必须查看UTF8中脚本生成的页面(您应该使用header()命令发送内容类型的标头,该标头指定这个)。

If any link in the chain is not UTF8, you'll end up getting junk displayed for certain character sequences that are valid in one character encoding, but not another. 如果链中的任何链接不是UTF8,那么对于某些字符序列而言,它们最终将显示为垃圾,这些字符序列在一种字符编码中有效,而在另一种字符编码中无效。 Whilst PHP has functionality for converting between charsets, it gets messy quickly and is best avoided. 虽然PHP具有在字符集之间进行转换的功能,但它会很快变得混乱,最好避免使用。

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

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