Page 1 of 1

Latest rows

Posted: Wed Aug 11, 2004 8:57 am
by dwfait
Hi. Im making a simple news posting system, but im stuck on one thing. How would i make it display the latest 5 (or any other user specified number) posts from a database?

Posted: Wed Aug 11, 2004 9:31 am
by CoderGoblin
Include a timestamp within the database on insertion.
Then use SELECT x from y ORDER BY timestamp DESC LIMIT 5

Posted: Wed Aug 11, 2004 11:21 am
by dwfait
im giving each news item a unique ID, which is auto-incrementated, so should i use that?

Also, how would i get the different fields for each result?

Posted: Wed Aug 11, 2004 11:50 am
by feyd

Code: Select all

SELECT * FROM your_table_name ORDER BY `ID` DESC LIMIT 5
where "your_table_name" is your table name, and ID is your unique id field name. That should handle it.

Posted: Wed Aug 11, 2004 1:03 pm
by dwfait
Thanks. Then how would i get the different fields from each of the results consecutivly?

Posted: Wed Aug 11, 2004 1:09 pm
by feyd
all fields of the row are returned from my query.. you can use [php_man]mysql_fetch_assoc[/php_man]() to retrieve each row in a while loop like so:

Code: Select all

<?php

$query = mysql_query('SELECT * FROM your_table_name ORDER BY `ID` DESC LIMIT 5') or die(mysql_error());

while($row = mysql_fetch_assoc($query))
{
  print_r($row);
}

?>

Posted: Wed Aug 11, 2004 1:32 pm
by m3mn0n
http://www.php.net/mysql

There is good code samples and more functions there.

Posted: Wed Aug 11, 2004 1:48 pm
by dwfait
How would i get those variables out of the array and into strings?

Posted: Wed Aug 11, 2004 1:56 pm
by feyd
$row['field_name']

Posted: Wed Aug 11, 2004 2:01 pm
by m3mn0n