Home >Backend Development >PHP Tutorial >菜鸟 问下PHP取出MYSQL数据,然后倒置顺序

菜鸟 问下PHP取出MYSQL数据,然后倒置顺序

WBOY
WBOYOriginal
2016-06-23 13:40:521150browse

mysql数据


php代码:

$res_ip = mysql_query("select * from ip  order by id DESC limit 0,5");while($row_ip = mysql_fetch_array($res_ip)){    $ip_ip[] = intval($row_ip['ip']);	$time_ip[] = date($row_ip['time']);}$data_ip = array(array("name"=>"IP流量","data"=>$ip_ip));$data_ip = json_encode($data_ip);$times_ip = json_encode($time_ip);$times_ip = str_replace('2015-', '', $times_ip);


结果:
["01-25","01-25","01-24","01-23","01-22"]

[{"name":"IP\u6d41\u91cf","data":[12632220,12735020,127350,2213488,2348888]}]


想要的效果:
["01-22","01-23","01-24","01-25","01-26"]

[{"name":"IP\u6d41\u91cf","data":[2348888,2213488,127350,12735020,12632220]}]


说明: 就是相当于 把取出的最新5条数据,倒置过来。正常的取出来的5条最新数据是:5 4 3 2 1. 想要的是 1 2 3 4 5。 5 是最新的数据。

致谢!


回复讨论(解决方案)

用id排序! 递减或者递增!你这个应该是递增!

select * from ip  order by id ASC limit 0,5 ?

id desc 改成 id asc 是不行的!
id desc 的本意是取最新数据,而 id asc 是取最旧的数据
要用 array_reverse 转置数组

$data_ip = array(array("name"=>"IP流量","data"=> array_reverse($ip_ip)));$data_ip = json_encode($data_ip);$times_ip = json_encode(array_reverse($time_ip));$times_ip = str_replace('2015-', '', $times_ip);

id排序应该就可以了啊,这个应该是按时间顺序写入的吧?

id排序应该就可以了啊,这个应该是按时间顺序写入的吧?



ASC 不行的,取出的是旧数据。

id desc 改成 id asc 是不行的!
id desc 的本意是取最新数据,而 id asc 是取最旧的数据
要用 array_reverse 转置数组

$data_ip = array(array("name"=>"IP流量","data"=> array_reverse($ip_ip)));$data_ip = json_encode($data_ip);$times_ip = json_encode(array_reverse($time_ip));$times_ip = str_replace('2015-', '', $times_ip);




斑竹的方法可行,谢谢
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn