Page 1 of 1

Load last 3 items from database

Posted: Wed Nov 02, 2011 7:31 pm
by Livengood
How would you load the last three items from a database using php?

Re: Load last 3 items from database

Posted: Wed Nov 02, 2011 8:00 pm
by Celauran

Code: Select all

SELECT whatever FROM tablename ORDER BY id DESC LIMIT 3

Re: Load last 3 items from database

Posted: Wed Nov 02, 2011 8:12 pm
by twinedev

Code: Select all

$aryData = array();
$rsData = mysql_query('SELECT * FROM tblName ORDER BY fldName DESC LIMIT 3');
if ($rsData && mysql_num_rows($rsData) > 0) {
    while ($aryData[] = mysql_fetch_assoc($rsData));
    array_pop($aryData); // Last item will be FALSE, not a record
    $aryData = array_reverse($aryData);
    mysql_free_resouce($rsData);
}
unset($rsData);

// $aryData = Last three results
or another way

Code: Select all

$aryData = array();
$rsData = mysql_query('SELECT * FROM tblName ORDER BY fldName DESC LIMIT 3');
if ($rsData) {
    $intRows = mysql_num_rows($rsData)
    if ($intRows>0) {
        for($t=$intRows-1; $t>=0; $t--) {
            $aryData[$t] = mysql_fetch_assoc($rsData);
        }
        mysql_free_resouce($rsData);
    }
}
unset($rsData);

// $aryData = Last three results

Re: Load last 3 items from database

Posted: Thu Nov 03, 2011 7:43 pm
by Livengood
Thanks guys, thats helps alot with this project :D