获取最后输入行的AUTO_INCREMENT


Get AUTO_INCREMENT of Last Inputted Row

>我有一个简单的问题。

正在使用PHP将信息放入MySQL中,并且我有一个auto_increment字段,我将独自离开。

如何获取刚刚输入的行中auto_increment字段的值?

你来了:

mysql_insert_id()

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

来自 php.net/mysql_insert_id

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d'n", mysql_insert_id());

PDO::lastInsertId — 返回上次插入的行或序列值的 ID

PDO::lastInsertId()

这里有三种不同的方法,来自三种可以与MySQL接口的常用库(mysql,mysqli,pdo)

mysql_insert_id(...) [程序功能]

mysqli::$insert_id [OOP 属性]

PDO::lastInsertId(...) [OOP 方法]

SELECT MAX(that_field) AS last_auto_increment_value FROM that_table;

SELECT auto_increment -1 AS last_auto_increment_value
FROM information_schema.tables 
WHERE table_name='that_table'
AND table_schema = 'that_database';

mysql_insert_id() //in PHP