Load last 3 items from database
Posted: Wed Nov 02, 2011 7:31 pm
How would you load the last three items from a database using php?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
SELECT whatever FROM tablename ORDER BY id DESC LIMIT 3Code: 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 resultsCode: 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