mysql - PHP get last entry


So I need to find the last entry of my MySql table, I get the first one this way:

if($f4==0) { /*do this*/ } else { /*or do that*/ }

I have a table with incremented IDs, but it is sorted out by position like this:

$query="SELECT * FROM promotions ORDER BY position";

I would basicly need something like:

if($f4==Last row in table) { /*Do this*/ } else { /*Or do that*/ }



If your table unique auto_increment column namedid , you can just accomplish your task like this =>

SELECT * FROM table WHERE id=(SELECT MAX(id) FROM table)



Well, you should use SORT (DESC) and LIMIT 1 to get a specific row.

As in Matts comment, it is good practice to use a auto imcrement column (mostly primary key) for this (alternative: timestamp/rowversion)

The more costly version is an array of all values and get the total amount of fetched values via mysql_num_rows() and then just access the row from the array with the corresponding ID (-1 because it starts at 0 and not at 1 like the mysql_num_rows function)




With a so vague question is difficult to see what you want to do really. Please provide some more code, don't worry, we won't steal it (;

In the meanwhile, you might want to see the MAX(), which could be useful.

People are also looking for solutions to the problem: php - How to set user's permission using custom form in Wordpress?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.