Jump to content

List from MySQL in newest first?


Sami

Recommended Posts

I want to list something from a database, but I'm getting the oldest first. How can I change that?I'm using:

$result = mysql_query("SELECT * FROM comments");while($row = mysql_fetch_array($result))  {  echo "<p><table border=\"1\" style=\"width:100%;\"><tr><th style=\"background-color:gray; color:white;\"> Skrevet av " . $row['name'] . " den " . $row['date'] . "</th></tr><tr><td>" . $row['comment'] . "</td></tr></table></p>";  }

Link to comment
Share on other sites

I do not know much about that, but I am pretty sure you can use the ORDER BY.Then it could look something like this

$result = mysql_query("SELECT * FROM comments ORDER BY date");while($row = mysql_fetch_array($result)){echo "<p><table border=\"1\" style=\"width:100%;\"><tr><th style=\"background-color:gray; color:white;\"> Skrevet av " . $row['name'] . " den " . $row['date'] . "</th></tr><tr><td>" . $row['comment'] . "</td></tr></table></p>";}

Which is my guess that it would then use your date row to sort the data, you could then also try ORDER BY date DESC or ORDER BY date ASC.I THINK that might work.

Link to comment
Share on other sites

I do not know much about that, but I am pretty sure you can use the ORDER BY.Then it could look something like this
$result = mysql_query("SELECT * FROM comments ORDER BY date");while($row = mysql_fetch_array($result)){echo "<p><table border=\"1\" style=\"width:100%;\"><tr><th style=\"background-color:gray; color:white;\"> Skrevet av " . $row['name'] . " den " . $row['date'] . "</th></tr><tr><td>" . $row['comment'] . "</td></tr></table></p>";}

Which is my guess that it would then use your date row to sort the data, you could then also try ORDER BY date DESC or ORDER BY date ASC.I THINK that might work.

Tank you :)Btw, it's still oldest first, but now I've found out that it helps against randomly.But how can I make it show the newest first?
Link to comment
Share on other sites

Newest first:

ORDER BY date ASC

Oldest first:

ORDER BY date DESC

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...