-3

For a project a need the echo the last 4 database records. I have a script that show all the records but I need only the last 4. Can someone help me?

<?php
$con = mysql_connect("localhost","test","test");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM formulier");

echo "<table border='1'>
<tr>
<th>tafel</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['tafel'] . "</td>";

  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Thanks!

Jamezz
  • 21
  • 1
  • 2
    just add "limit 4" (and "order by ..." if you need to specify a field for order) in the mysql query. – nanocv Apr 02 '18 at 07:52
  • 1
    It is inconceivable that a project would employ a deprecated API – Strawberry Apr 02 '18 at 07:53
  • Do you have a column that can be used to check for the timeline or the order of the data? – Carl Binalla Apr 02 '18 at 07:54
  • Nanocv, Thanks! SELECT * FROM formulier LIMIT 4"); is for the first 4 records.I need the last 4 records. SELECT * FROM your_dataBase ORDER BY id DESC LIMIT 4 does not work but thanks for the command – Jamezz Apr 02 '18 at 08:00
  • I made a mistake SELECT * FROM your_dataBase ORDER BY id DESC LIMIT 4 is good. Thanks! – Jamezz Apr 02 '18 at 08:15

1 Answers1

1

try this query... SELECT * FROM your_dataBase ORDER BY id DESC LIMIT 4

sk sakil
  • 186
  • 1
  • 12