PHP知识记录

1、解决数据库前台显示乱码问题

在mysql_connect后面加一句SET NAMES UTF8,即可使得UTF8的数据库消除乱码,对于GBK的数据库则使用SET NAMES GBK,代码如下:

1$mysql_mylink= mysql_connect($mysql_host, $mysql_user, $mysql_pass);
2mysql_query("SET NAMES 'GBK'");

数据库字符集为utf-8 

1mysql_query("SET NAMES 'UTF8'");
2mysql_query("SETCHARACTERSET UTF8");
3mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");



2、从数据库返回数据封装成Json发送

<?php
$con = mysql_connect ( "localhost", "root", "root" );

mysql_query ( "SET NAMES 'UTF8'" );
mysql_query ( "SET CHARACTER SET UTF8" );
mysql_query ( "SET CHARACTER_SET_RESULTS=UTF8'" );
$db = mysql_select_db ( "dazhong" );
$sql = "select*from shop";
$result = mysql_query ( $sql );
$arr = array ();
while ( $r = mysql_fetch_assoc ( $result ) ) {
	$arr[]=$r;
}
echo count($arr);
echo "<hr >";
echo json_encode($arr);
?>


你可能感兴趣的:(数据库,安卓,乱码,utf-8,character)