mysql汉语言无法读取。

WBOY
Release: 2016-06-13 11:50:19
Original
903 people have browsed it

mysql中文无法读取。。。

这是mysql数据库的编码方式

这是后台数据的结果,不使用中文的话会正常写入到后台,使用中文的话就不能被写入。(注意看倒数几排数据)
直接向数据库写入中文的话,会显示出来。
PHP代码部分
if(isset($_POST['B1']))
{
$username=$_POST["username"];
$message=$_POST["message"];
if($username=="")
{
echo "<script> alert('请输入您的姓名!');</script>";
if($message=="")
{
echo "<script> alert('请输入留言内容!');</script>";
}
}
else
{
$addtime=date("y-m-d h:m:s");
$id=mysql_connect("localhost","root","1234");
mysql_select_db("gbook",$id);
$query="insert into message(author,addtime,content,reply) values('$username','$addtime','$message','')";
$result=mysql_query($query,$id);
mysql_close($id);
//echo "result=".$result;
echo "<script> alert('留言成功,点击确定查看留言!');location.href='index.php';</script>";
}
}
------解决方案--------------------

<br />$username=iconv('gb2312','utf-8',$_POST["username"]);<br />$message=iconv('gb2312','utf-8',$_POST["message"]);
Copy after login

------解决方案--------------------
数据库编码是 utf8
直接向数据库写入中文的话,会显示出来。(截图追后一行)
phpmyadmin 会做编码设置,自然没有问题
你的代码没有做编码设置,所以出现问题是很正常的!
当文件编码是 gbk (windows 下的 ANSI)时应有
mysql_query('set names gbk');
当文件编码是 utf-8 时应有
mysql_query('set names utf8');


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!