-4

The following code selects the last 10 rows from my table, but outputs them in reverse order because it's descending. How can i reverse that? Will I need to swap the array around in PHP or can this be done with the SQL statement?

$result = mysql_query("SELECT * FROM chat ORDER BY ID DESC LIMIT 10") ;

So how can I swap it around so I get the most recent row first from my selected 10? This is more of a case of swapping the results around rather than getting last 10 rows, my code does that...

FIXED:

mysql_query("SELECT * FROM (SELECT * FROM chat ORDER BY ID DESC LIMIT 10) chat ORDER BY ID");
bobster
  • 59
  • 4
  • 11
  • 2
    dupe dupe dupe ... see related column on the right... – ficuscr Jan 11 '13 at 20:21
  • bobster, it's better coding methodology to specify the column names rather than put the burden on the server to loop through all columns using *. – user3871 Jan 11 '13 at 20:25
  • @Growler: it's not so much about the "burden on the server" (that's really a serious micro-optimisation), but more the effect of schema changes on code that expects a particular structure to the resultset. – eggyal Jan 11 '13 at 20:26
  • @eggyal good to know. I'm still very new to the scene too :) – user3871 Jan 11 '13 at 20:27
  • @eggyal btw How does * change the schema? Isn't "*" synonymous with a loop through all column names? – user3871 Jan 11 '13 at 20:35
  • @eggyal I would think that each time you rename a column/change column order, '*' can just iterate through the newly presented dataset (update what it iterates over). – user3871 Jan 11 '13 at 20:40
  • @Growler: See http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select – eggyal Jan 11 '13 at 20:46
  • @bobster For new code it is better to use mysqli instead of mysql, because from php 5.5.0 onwards mysql is depreciated. See the manual for further reference http://de3.php.net/manual/en/intro.mysql.php – Mr. Radical Jan 12 '13 at 20:44

2 Answers2

2
SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 10) t ORDER BY ID
eggyal
  • 122,705
  • 18
  • 212
  • 237
  • " t ORDER BY ID" <-- Is that still SQL query? – bobster Jan 11 '13 at 20:27
  • $result = mysql_query("SELECT * FROM chat ORDER BY ID DESC LIMIT 10") t ORDER BY ID ; That's the line that gives an error. – bobster Jan 11 '13 at 20:38
  • Use the query from eggyal and change t into table_name, like so: SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 10) table_name ORDER BY ID . – Mr. Radical Jan 11 '13 at 20:39
  • Now I get "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in posts.php on line 12" – bobster Jan 11 '13 at 20:47
  • Could inserting AS after LIMIT 10 help? "SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 10) AS t ORDER BY ID;" I think this question has some similarities. http://stackoverflow.com/questions/573646/mysql-select-from-n-last-rows – Mr. Radical Jan 11 '13 at 20:48
  • I'll add my PHP into my post now, hang on. – bobster Jan 11 '13 at 20:53
1

You can use the PHP function array_reverse, as described here: http://de3.php.net/manual/de/function.array-reverse.php

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24