我有一张桌子.主键为id,其自动递增. 现在,当我插入新记录时,我需要获取用于更新记录的ID. 我怎样才能做到这一点?如果我使用查询...
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...执行后,我可以获得ID.但是我可以确定它是刚刚插入的记录的ID吗?因为很多人会同时使用该应用程序.
...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.
我正在使用php和mysql.
I am using php and mysql.
Ayone可以在mysql中为我提供一个示例代码片段吗?
Can ayone provie me a sample code snippet in php with mysql?
推荐答案如果要从自动增量列中查找最新ID,只需运行 mysql_insert_id() 函数生成的ID查询.
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.
像这样:
//--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();现在$newest_id将包含插入的最新行的ID.
And now $newest_id will contain the ID of the latest row inserted.
更多推荐
MYSQL:SQL查询以获取autoincrement字段的值
发布评论