在学习PHP,遇到mysql输出中文乱码,经过N次尝试,找到了解决php调用mysql中文时显示乱码的解决方法:
首先在第一行加入 <?php header("content-type:text/html; charset=utf-8");?>
在连接数据库的第一行加入一行代码:
首先在第一行加入 <?php header("content-type:text/html; charset=utf-8");?>
在连接数据库的第一行加入一行代码:
<?php
...
$conn = new mysqli("localhost","root","password","databasename");
$conn->query("set names 'utf8'");
...
?>
测试一下,乱码问题解决。
...
$conn = new mysqli("localhost","root","password","databasename");
$conn->query("set names 'utf8'");
...
?>
注意:老修用的是mysqli类来连接数据库的。普通连接方式请用:mysql_query("set names 'utf8'");
按照以上方法可还是出现乱码,于是将mysql_query("set names 'utf8'");改为mysql_query("set names 'GBK'");或mysql_query("set names 'GB2312'");一切OK了,哈哈……