Thursday, 22 August 2013

How to show only last 5 rows from DataBase using foreach

How to show only last 5 rows from DataBase using foreach

Hi i have database table called Log, I want to show only the last 5 rows
from this dabatase . Here my code :
php :
function get_log_data(){
$query = "SELECT * FROM log";
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query, $this->connection);
if(!$result) //ERROR IN YOUR SQL QUERY
return false;
if(mysql_num_rows($result)==0) //NO ROWS IN TABLE pages
return false;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) // CHANGE THE
RESULT TO 2 DIMMENSIONAL ARRAY
{
$returned_array[] = $row;
}
return $returned_array;
}
on the html :
$log_data = $func->get_log_data();
<table cellspacing="0" cellpadding="0" style="width: 100%">
<?php
foreach($log_data as $key=>$value)
{
echo '<tr >';
echo '<td class="count"
style="width:20px;text-align:center;">.</td><td>'.
$value['action'].' : '.$value['user'].' .</td>';
echo '</tr >';
}
?>
</table>
i want to foreach only 5 last and no more Thanks.

No comments:

Post a Comment