插入到mysql的是中文,但select的时候显示乱码
+--------------------------+--------+|Variable_name|Value|+--------------------------...
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
应该是字符集的错误,请问怎么更改啊
我百度了之后,修改了,但是重启mysql之后,又变回成这样,求助! 展开
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
应该是字符集的错误,请问怎么更改啊
我百度了之后,修改了,但是重启mysql之后,又变回成这样,求助! 展开
2个回答
展开全部
我以前自学php的时候也被这个问题挠头过,希望楼主能熬过这段时间!!
解决方案:
1. 解决html乱码
要设置<meta http-equiv="Content-Type" content="text/html; charset=uft8">。
-------------------------------------------------------------------------------------------------------
2. 解决php页面乱码
建议在PHP页面头部写: header("Content-type: text/html; charset=utf-8");
<?php
//引入smarty配置文件
//smarty出现乱码解决方案: 重写truncate截取字符的方法
header("Content-type: text/html; charset=utf-8");
include_once("smarty_conn.php");
$name = "PHP100视频教程";
//数组只能是默认的数字索引
$arry = array("陈进",22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$smarty->assign("title",$name);
$smarty->assign("arry",$arry);
$smarty->assign("t_array",$arr);
$smarty->display("index.html");
?>
-------------------------------------------------------------------------------------------------------
3.解决mysql乱码
在数据库插入的PHP页面写:mysql_query("set names 'gbk'"),
另外建议数据库DB字符集为utf8, 建表的时候 最后请写上: default charset=utf8;
如此永远不会出现乱码了
<?php
define('CLIENT_MULTI_RESULTS', 131072);
$mysql_server_name="localhost";
$mysql_username="root";
$mysql_userpwd="123456";
$mysql_database="news_DB";
$link=@mysql_connect($mysql_server_name,$mysql_username,$mysql_userpwd,CLIENT_MULTI_RESULTS)
or die("Could not connect: ".mysql_error());
mysql_select_db($mysql_database,$link) or die("Could not select database");
mysql_query("set names 'gbk'");//使用中文编码
//php写入mysql的编码,防止mysql出现乱码
//一个替换函数,替换换行和回车
function htmtocode($content) {
$content = str_replace("\n","<br/>",str_replace(" "," ",$content));
return $content;
}
?>
希望我的回答能帮助楼主,祝你学习更上一层楼
解决方案:
1. 解决html乱码
要设置<meta http-equiv="Content-Type" content="text/html; charset=uft8">。
-------------------------------------------------------------------------------------------------------
2. 解决php页面乱码
建议在PHP页面头部写: header("Content-type: text/html; charset=utf-8");
<?php
//引入smarty配置文件
//smarty出现乱码解决方案: 重写truncate截取字符的方法
header("Content-type: text/html; charset=utf-8");
include_once("smarty_conn.php");
$name = "PHP100视频教程";
//数组只能是默认的数字索引
$arry = array("陈进",22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$arr[] = array("name"=>"陈进","age"=>22);
$smarty->assign("title",$name);
$smarty->assign("arry",$arry);
$smarty->assign("t_array",$arr);
$smarty->display("index.html");
?>
-------------------------------------------------------------------------------------------------------
3.解决mysql乱码
在数据库插入的PHP页面写:mysql_query("set names 'gbk'"),
另外建议数据库DB字符集为utf8, 建表的时候 最后请写上: default charset=utf8;
如此永远不会出现乱码了
<?php
define('CLIENT_MULTI_RESULTS', 131072);
$mysql_server_name="localhost";
$mysql_username="root";
$mysql_userpwd="123456";
$mysql_database="news_DB";
$link=@mysql_connect($mysql_server_name,$mysql_username,$mysql_userpwd,CLIENT_MULTI_RESULTS)
or die("Could not connect: ".mysql_error());
mysql_select_db($mysql_database,$link) or die("Could not select database");
mysql_query("set names 'gbk'");//使用中文编码
//php写入mysql的编码,防止mysql出现乱码
//一个替换函数,替换换行和回车
function htmtocode($content) {
$content = str_replace("\n","<br/>",str_replace(" "," ",$content));
return $content;
}
?>
希望我的回答能帮助楼主,祝你学习更上一层楼
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询