Nawaz Nawaz - 1 month ago 11
JSON Question

How to fetch mysql data in descending order?

So my application gets mysql data in json format and displays it in
id1 to id*
I have to update my database table every day and I want to show the recent data first, I don't want to change the entire table structure each and every time I update my database.

Is there any way that I can add rows in ascending order and fetch data in descending order so that my app will show the fresh data first?

here is the encoder php

<?php

$connection = mysqli_connect("domain","database user","password","database name");
$id = $_GET["id"];
$query = "Select * from my_data where id between ($id+1) and ($id+10)";
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {

$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);

?>

Answer

use order by desc like this

Select * from my_data where id between ($id+1) and ($id+10) order by id desc

Here your data shorted in descending order. and order follow in id. if you want to do order with any other field. than you can give name of field instead of id.