Cainiao asks PHP to retrieve MYSQL data and then invert the order-php Tutorial

Source: Internet
Author: User
Cainiao asks PHP to retrieve MYSQL data and then invert the order of mysql data


Php code:
$ 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 ("name" => "IP traffic", "data" => $ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );


Result:
["01-25","01-25","01-24","01-23","01-22"]

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


Expected results:
["01-22","01-23","01-24","01-25","01-26"]

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


Note: This is equivalent to putting the latest 5 data records into the inverted structure. Normally, the 5 latest data records are: 5 4 3 2 1. what you want is 1 2 3 4 5. 5. The latest data.

Thank you!


Reply to discussion (solution)

Sort by id! Decrease or increase! You should be increasing!

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

Changing id desc to id asc won't work!
Id desc is designed to get the latest data, while id asc is the oldest data.
Use array_reverse to transpose an array

$ Data_ip = array ("name" => "IP traffic", "data" => array_reverse ($ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode (array_reverse ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );

Id sorting should be enough. should this be written in chronological order?

Id sorting should be enough. should this be written in chronological order?



ASC does not work. The old data is retrieved.

Changing id desc to id asc won't work!
Id desc is designed to get the latest data, while id asc is the oldest data.
Use array_reverse to transpose an array

$ Data_ip = array ("name" => "IP traffic", "data" => array_reverse ($ ip_ip); $ data_ip = json_encode ($ data_ip ); $ times_ip = json_encode (array_reverse ($ time_ip); $ times_ip = str_replace ('2017-', '', $ times_ip );




This method is feasible. thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.