当前位置: 代码迷 >> PHP >> mysqli_connect_error 乱码解决思路
  详细解决方案

mysqli_connect_error 乱码解决思路

热度:481   发布时间:2016-04-28 19:00:23.0
mysqli_connect_error 乱码
代码如下
header("Content-Type: text/html; charset=utf-8");
$db_host='127.0.0.1';
$db_host='192.168.0.22';
$db_user='root';
$db_pwd='';
$db_name='iq1';
@$mysqli=new mysqli($db_host,$db_user,$db_pwd,$db_name);

if (mysqli_connect_errno()){
$str='<fieldset><legend>数据库连接失败</legend>';
$str.="<p>可能的原因有:数据库防火墙未关闭、数据库服务未启用</p>";
$str.="<p>返回的错误信息是 %s</p>";
$str.="</fieldset>";
printf ($str,mysqli_connect_error());
exit();
};
?>

 文件编码为UTF-8 

浏览器显示 结果为


移除
header("Content-Type: text/html; charset=utf-8");


将文件保存为 GB2312 页面显示正常,求解?
------解决方案--------------------
那是因为 mysqli_connect_error() 返回的信息是 gbk 的
你要 utf-8 的话就转一下码
printf ($str, iconv('gbk', 'utf-8', mysqli_connect_error()));