This question has already been answered many times: MySQL: LAST_INSERT_ID () returns 0
You use this function out of context. It will only work if you inserted a line immediately before this:
INSERT INTO 'subscription' (name) VALUES ('John Smith'); SELECT LAST_INSERT_ID() FROM subscription
However, you can select the row with the highest identifier, which logically will be the most recently added ...
SELECT MAX( id ) FROM subscription;
However, the standard approach is to simply call mysqli_insert_id or mysql_insert_id (depending on whether you are using the mysqli or mysql mysql library.) I must add that the mysql library is very discouraged because it is almost completely deprecated). Here everything will look like this:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); $mysqli->query("INSERT INTO 'subscription' (name) VALUES ('John Smith');"); printf ("New Record has id %d.\n", $mysqli->insert_id);
If you did not enter the subscription in the same script as collecting the most recent row identifier, use the "select max" approach. This seems unlikely if you mentioned '1 script'
Also, if your identifier is not consistent or you do not have an identifier field, or you have a row identifier other than the one you just added, you should probably look at the "date_added" column to determine which one really was the last one. However, these scenarios are unlikely.
source share