My version of MySql breaks when I try to run the statements suggested by johanvw. Maybe I am brain-dead, but I could not come up with a way to do this with MySql with a single SQL statement, I had to do it with two sql statements.
The code I used is not very elegant and there is no doubt plenty of room for improvement, but I figured as long as it worked anyway I would toss it up for discussion, improvement, whatever. I would certainly be interested knowing a cleanerr way, but if someone comes up with one I will prolly be slapping my forehead and going, "OF COURSE!"
This should work as a stand alone PHP page by changing the variables at the top of the page:
<?php
//(example only, but the syntax has been tested)
$table="table"; /** name of your table **/
$table_id="id"; /** name of your table index **/
$mysql_host = "localhost";/** your local path **/
$mysql_user = "root"; /** your mysql user name **/
$mysql_pass = ""; /** your mysql user password **/
$mysql_base = "mysql";/** name of mysql database **/
$mysql_link = mysql_connect
($mysql_host,$mysql_user,$mysql_pass)
or die (mysql_errno().": ".mysql_error());
mysql_select_db($mysql_base)
or die ("could not connect to db<p>".
mysql_errno().": ".mysql_error());
//first sql query collects highest number to view
$sql="select $table_id
from $table
order by $table_id desc
limit 6
";
$result=mysql_query($sql)
or die ("<p>$sql<p>".mysql_error());
$i=0;
while($row=mysql_fetch_array($result, MYSQL_BOTH)){
$i++;
if ($i==6) $max_row = $row[$table_id];
}
//second sql statement collects all but most recent 5
$sql="select *
from $table
where $table_id <= $max_row
order by $table_id desc
";
$result=mysql_query($sql)
or die ("<p>$sql<p>".mysql_error());
//now display all the results
$fields=mysql_num_fields($result);
echo "<table><tr>";
for ( $i=0; $i < $fields; $i++){
echo "<th>";
echo mysql_field_name($result, $i);
echo "</th>";
}
echo "</tr>";
while($row=mysql_fetch_array($result, MYSQL_BOTH)){
echo "<tr>";
for ( $i=0; $i < $fields; $i++){
echo "<td>$row[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
?>