joeytrumann joeytrumann - 7 months ago 21
SQL Question

How can I get the last row from a table mysqli?

I have a table and am trying to get the most recent row using this code:

include "db_conx.php";

$sql="SELECT column FROM table ORDER BY DESC LIMIT 1";

if ($result=mysqli_query($db_conx,$sql))
{
while ($row=mysqli_fetch_row($result))
{
printf($row[0]);
}
mysqli_free_result($result);
}


It returns a blank result though.

Answer
ORDER BY DESC LIMIT 1

ORDER BY what DESC? You have to provide a column name where which you want to order by. Could be an Auto increment column, primary key or even the timestamp etc

As it currently stands your query has invalid syntax and will not return anything other than an error.

Comments