我想知道在mysql查询后获取最后一个插入ID的最佳解决方案是什么?
我找到了以下解决方案:
PHP
function get_current_insert_id($table)
{
$q = "SELECT LAST_INSERT_ID() FROM $table";
return MysqL_num_rows(MysqL_query($q)) + 1;
}
?>
甚至使用MysqL_insert_id PHP函数,但显然这个函数不能很好地与bigint(这就是我用于ID字段)和如果有很多连续的SQL查询它可能是不可靠的.
最佳答案
SELECT LAST_INSERT_ID()不可靠且足够安全吗?
来自MysqL Doc:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients,even if they generate AUTO_INCREMENT values of their own.