PHP & MySQL find the last entry in the database

So you are looking for the last entry in a table for your database that has auto increment.  Well I have this to share with you. Use the mysql_insert_id() function.

1
2
mysql_query("INSERT INTO table1 (product) values ('$somethin')");
$lastID = mysql_insert_id();

You need to combine the insert command and then use the mysql_insert_id function to create a variable that holds the id number of the entry that was just generated.

You can then call the last id by using the variable. e.g.

1
echo $lastID;

You could use it better by quering the database and use a where clause.

1
2
3
4
5
6
$query = "SELECT * FROM table1 WHERE id=$lastID"; //select from DB image url and set order
	$result = mysql_query ($query);
 
	while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
	echo ($row['something']. "</br>");
	}
1