PHP mysql_insert_id() 在bigint型AI字段遇到的问题

阅读(3058)

若数据表中定义了某字段为自增ID,且类型为bigint,则不能使用 mysql_insert_id() 来获取最近一次生成的值。

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

mysql_insert_id() 将 MySQL 内部的 C API 函数 mysql_insert_id() 的返回值转换成 long(PHP 中命名为 int)。如果 AUTO_INCREMENT 的列的类型是 BIGINT,则 mysql_insert_id() 返回的值将不正确。可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID() 来替代。

简言之,mysql_insert_id()做了类型转换,就不对了。

同时,此方法在PHP5.0后也被废弃了;建议使用mysqli_insert_id() 或 PDO::lastInsertId(),则无此问题。

Tags: mysql