2

I am looking for a php function which returns the most recent entry into a mysql table.

1
  • 4
    its called google: mysql_query for most recent line... Commented Apr 11, 2011 at 15:59

4 Answers 4

2

You can use mysql_insert_id() - http://php.net/manual/en/function.mysql-insert-id.php, which returns the most recent key from the database. You could then do a query to select it. For example

$id = mysql_insert_id(); $result = mysql_query("SELECT * FROM table WHERE id = ".$id);

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

2

Get the last row, assuming id is the primary key (with auto increment):

mysql_fetch_array(mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 1));

1 Comment

Same as Postive's answer, therefore +1'd this too. Thanks for the help :)
2

You can use

mysql_inset_id to retrieve recently added rows id.

But that should be used to immediately after the insert query.

mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

If you want to retrieve it later you can use a select query

select * from table  order by id desc limit 1;

1 Comment

This was the answer that I was looking for. Thanks, Positive. +1
1

int mysql_insert_id ([ resource $link_identifier ] )

http://php.net/manual/en/function.mysql-insert-id.php

Maybe thats what you looking for

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.