I have a table. The primary key is id and its auto incremented.
Now when I insert a new record, I need to get the id with which the record was updated.
How can I do that? If i use the query...
select max(id) from table_name
...after executing I can get the id. But can I be sure that its the id of the record that was just inserted? Because many people will be using the app at same time.
I am using php and mysql.
Can ayone provie me a sample code snippet in php with mysql?
解决方案
If you want to find out what the newest ID from an auto increment column is, you just have to run the mysql_insert_id() function right after your insert query.
Like so:
//--connection already made to database
//--now run your INSERT query on the table
mysql_query("INSERT INTO table_name (foo_column) VALUES ('everlong')");
//-- right after that previous query, you run this
$newest_id = mysql_insert_id();
And now $newest_id will contain the ID of the latest row inserted.