PHP查询到的数据存放到数组里面,一般使用$arr[]=$row的方式实现,$row是mysql_fetch_array获得的一行数据,本身是一个数组,执行上面的语句之后,这一行会添加存放在额为数组$arr的最后。
创新互联公司网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、做网站、成都网站设计易于使用并且具有良好的响应性。
典型的例子代码是这样的:
mysql_connect('127.0.0.1', 'root', '123456');
$sql='select * from test.tab';
if ($res=mysql_query($sql)){
while($row=mysql_fetch_array($res)) $result[]=$row;
mysql_free_resule($res);
}else echo "执行SQL语句:$sqlbr\n错误:".mysql_error();
echo '查询结果在下面的额为数组里面:pre';
print_r($result);
echo '/pre';
你给出的数据组织形式,貌似是json,那么你可以直接用json_encode() 和 json_decode()进行转换后,再进行处理。
示例代码:
?php
$json = '{
"6846": {
"userid": "6846",
"expertname": "三剑客",
"firstrate": "0",
"wrong": null,
"firstqs": "2011254",
"firstvalue": "2",
"lastrate": "0",
"firstnum": "0",
"lastnum": "0",
"kjtime": "2012-09-30 00:00:00",
"successrate": "0",
"curqs": " (2012267,2012266,2012262,2012259,2011256,2011255,2011254 )",
"lastpaiming": "1",
"curpaiming": "1"
},
"9205": {
"userid": "9205",
"expertname": "东方诸葛",
"firstrate": "0",
"wrong": null,
"firstqs": "2011254",
"firstvalue": "2",
"lastrate": "0",
"firstnum": "0",
"lastnum": "0",
"kjtime": "2012-09-30 00:00:00",
"successrate": "0",
"curqs": " (2012267,2012266,2012262,2012259,2011256,2011255,2011254 )",
"lastpaiming": "2",
"curpaiming": "2"
},
"9213": {
"userid": "9213",
"expertname": "神探老李",
"firstrate": "0",
"wrong": null,
"firstqs": "2011254",
"firstvalue": "2",
"lastrate": "0",
"firstnum": "0",
"lastnum": "0",
"kjtime": "2012-09-30 00:00:00",
"successrate": "0",
"curqs": " (2012267,2012266,2012262,2012259,2011256,2011255,2011254 )",
"lastpaiming": "3",
"curpaiming": "3"
},
"17518": {
"userid": "17518",
"expertname": "草原雄鹰",
"firstrate": "0",
"wrong": null,
"firstqs": "2011254",
"firstvalue": "2",
"lastrate": "0",
"firstnum": "0",
"lastnum": "0",
"kjtime": "2012-09-30 00:00:00",
"successrate": "0",
"curqs": " (2012267,2012266,2012262,2012259,2011256,2011255,2011254 )",
"lastpaiming": "4",
"curpaiming": "4"
}
}';
echo 'Pre';
print_r( json_decode($json,true) );
echo '/pre';
?
为了便于随时echo,存为字符串最好,一般的代码如下:
$sql='select * from xxx order by xxid desc limit 5';//limit 5表示只取5个,order by xxxid desc表示按xxxid降序排列,可以显示最新的5个
$res=mysql_query($sql);
$str5='';//保存的结果
while($row=mysql_fetch_array($res)) $str5.=$row[0].‘br';//可能你需要修改这一句,控制显示格式
mysql_free_result($res);
以后你就可以随时echo $str5了。
取这两条的时候,原数组已经同时删除了取出的结果,数组中剩下的就只有3条了