- PHP页面编码问题
PHP无法返回汉字,返回相应的汉字数据时出现乱码,这时只需要在代码开始处加入相应的代码。
php页面为utf编码
header("Content-type: text/html; charset=utf-8");
php页面为gbk编码
header("Content-type: text/html; charset=gb2312");
php页面为big5编码
header("Content-type: text/html; charset=big5");
- 数据不重复筛选—distinct用法和count函数
distinct关键字过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。
用count函数语句可以实现返回:
select *, count(distinct name) from table group by name
group by 必须放在 order by 和 limit之前,不然会报错
示例代码:
<?php
header("Content-type: text/html; charset=utf-8");
$con = mysql_connect("localhost","root","huweishen");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("home_server", $con);
$result = mysql_query("SELECT *,count(distinct Device) FROM message group by Device" );
while($row = mysql_fetch_array($result))
{
echo $row[Device];
}
mysql_close($con);
?>