Aravind Venkat Aravind Venkat - 1 month ago 6
PHP Question

how to get data from mysql (last to first order) and Display in JSON php?

This is my Php file:

$sql = "SELECT * FROM sample";

$r = $conn->query($sql);
$result = array();

while($row = $r->fetch(PDO::FETCH_OBJ)){

array_push($result,array(
"title"=>$row->title
));
}
echo json_encode(array('result'=>$result));


This code fetches the data from my database and display from first to last order, But what i have tried is to create reverse order (From last to first) but I can't do it. Please help

Answer

Use SELECT * FROM sample ORDER BY ID DESC

ID is the column, according to which you want to order.

Comments