-1

I want to order my posts from mysql. Newest at first.

Whit this code working fine, but oldest posts at first place

$sql = "SELECT * FROM post LIMIT $offset, $no_of_records_per_page";

I tried with this codes and i got an error " mysqli_fetch_object() expects parameter 1 to be mysqli_result, bool given"

$sql = "SELECT * FROM post LIMIT $offset, $no_of_records_per_page DESC";

or

$sql = "SELECT * FROM post LIMIT $offset, $no_of_records_per_page ORDER BY date DESC";

Im using pagination. How can i slove this?

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
mrmiaki
  • 21
  • 3
  • 1
    LIMIT goes *after* ORDER BY – Barmar Nov 20 '20 at 23:07
  • 1
    Any SQL tutorial should describe the order of clauses. It's not flexible. – Barmar Nov 20 '20 at 23:08
  • 1
    Also debugging tip: use `error_log($sql);` to see what the SQL query looks like _after_ variables are interpolated into it. I wonder if `$offset` and `$no_of_records_per_page` are set, and are they integers? – Bill Karwin Nov 20 '20 at 23:15
  • yes $no_of_records_per_page = 8; $offset = ($pageno-1) * $no_of_records_per_page; – mrmiaki Nov 20 '20 at 23:17

1 Answers1

3

The row-limiting syntax is ORDER BY ... LIMIT ....

So:

SELECT * FROM post ORDER BY date DESC LIMIT $offset, $no_of_records_per_page;
GMB
  • 216,147
  • 25
  • 84
  • 135