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 displays from first to last order, but I need to create a reverse order (from last to first). Please help.
Use SELECT * FROM sample ORDER BY ID DESC
ID is the column, according to which you want to order.
Add ORDER BY
and your column name in your query, followed by DESC
or ASC
(descending or ascending, the latter being the default). For example:
$sql = "SELECT * FROM sample ORDER BY title DESC";