PDA

View Full Version : Showing MySQL query results in descending orde?


antizero
10-16-02, 05:26 PM
Is it possible to show MySQL query results in descending primary key order? For my project, it would make more sense to show the newest entries first, but they would have the highest primary key ID numbers. Here's my current code, it's for an image archive:

<?PHP $db = @mysql_connect("localhost", "root");
mysql_select_db("poop",$db);
$color1 = "#ffffcc";
$color2 = "#99ccff";
$row_count = 0;
$result = mysql_query("SELECT * FROM pics",$db);
while ($row = mysql_fetch_array($result)) {
$picfile = $row["picfile"];
$picurl = $row["picurl"];
$ID = $row["ID"];
$picdesc = $row["picdesc"];
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr><td align=right bgcolor=$row_color nowrap><a class=c href=pics.php?ID=$ID>$picfile</a></td><td bgcolor=$row_color align=left>";
echo "$picdesc</td></tr>";
$row_count++;
}
?>


The result is a table with alternating color rows. $ID is the primary key, auto-incremented. I'd like to start with the highest primary key and work down, rather than vice versa.

CoderLaureate
10-16-02, 06:34 PM
Change your query to look something like this.

Select * from {table} order by {key field} desc

antizero
10-17-02, 01:40 AM
Bingo! Thanks!