Skip to content
Advertisement

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:

$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);

Advertisement

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement