当前位置:Gxlcms > PHP教程 > 【PDO扩展】lastInsertId函数返回0的原因

【PDO扩展】lastInsertId函数返回0的原因

时间:2021-07-01 10:21:17 帮助过:16人阅读

问题

在使用PHP的PDO扩展插入数据的时候,有时候需要获取到最后插入记录的ID作为返回信息。要怎么才能实现这个需求呢?

lastInsertId函数

使用PDO的lastInsertId函数。

但是,最近在使用的过程中发现有时候lastInsertId函数返回的是0。为什么会这样呢?

先来看看lastInsertId函数在 PHP手册 上的说明。

返回最后插入行的ID或序列值。

再来看看下面的几个例子。

测试例子

主键是ID字段,使用自增约束。

getMessage();    }    for( $i = 0; $i < 10; $i++){            $sql = 'INSERT INTO `tbl_test` (id, name) VALUE (:id, :name)';            $data = array(                    ':id' => '',                    ':name' => "user_$i"            );            $sth = $dbh->prepare($sql);            $sth->execute($data);    }    $sql = 'INSERT INTO `tbl_test` (id, name) VALUE (:id, :name)';    $new_data = array(            ':id' => '',            ':name' => 'user_new'    );    $sth = $dbh->prepare($sql);    $sth->execute($new_data);    $last_id = $dbh->lastInsertId();    echo 'last id: ' . $last_id;

结果

last id: 11

主键是ID字段,不使用自增约束。

getMessage();    }    for( $i = 0; $i < 10; $i++){            $sql = 'INSERT INTO `tbl_test` (id, name) VALUE (:id, :name)';            $data = array(                    ':id' => $i,                    ':name' => "user_$i"            );            $sth = $dbh->prepare($sql);            $sth->execute($data);    }    $sql = 'INSERT INTO `tbl_test` (id, name) VALUE (:id, :name)';    $new_data = array(            ':id' => '',            ':name' => 'user_new'    );    $sth = $dbh->prepare($sql);    $sth->execute($new_data);    $last_id = $dbh->lastInsertId();    echo 'last id: ' . $last_id;

结果

last id: 0

主键不是ID字段,主键使用自增约束。

getMessage();    }    for( $i = 0; $i < 10; $i++){            $sql = 'INSERT INTO `tbl_test` (tbl_id, name) VALUE (:tbl_id, :name)';            $data = array(                    ':tbl_id' => $i,                    ':name' => "user_$i"            );            $sth = $dbh->prepare($sql);            $sth->execute($data);    }    $sql = 'INSERT INTO `tbl_test` (tbl_id, name) VALUE (:tbl_id, :name)';    $new_data = array(            ':tbl_id' => '',            ':name' => 'user_new'    );    $sth = $dbh->prepare($sql);    $sth->execute($new_data);    $last_id = $dbh->lastInsertId();    echo 'last id: ' . $last_id;

结果

last id: 11

主键不是ID字段,不使用自增约束。

getMessage();    }    for( $i = 0; $i < 10; $i++){            $sql = 'INSERT INTO `tbl_test` (tbl_id, name) VALUE (:tbl_id, :name)';            $data = array(                    ':tbl_id' => uniqid(),                    ':name' => "user_$i"            );            $sth = $dbh->prepare($sql);            $sth->execute($data);    }    $sql = 'INSERT INTO `tbl_test` (tbl_id, name) VALUE (:tbl_id, :name)';    $new_data = array(            ':tbl_id' => uniqid(),            ':name' => 'user_new'    );    $sth = $dbh->prepare($sql);    $sth->execute($new_data);    $last_id = $dbh->lastInsertId();    echo 'last id: ' . $last_id;

结果

last id: 0

查看PHP源码

可以看到,有些例子返回0,有些例子返回最新的ID。那么lastInsertId什么情况下会返回0呢?在网上搜了很多资料,并没有发现想要的答案,翻开PHP源码,发现函数 last_insert_id 的实现源码是这样的:

可以看到,函数返回的id的值是调用mysql api中的 mysql_insert_id 函数返回的值。

查看mysql手册

翻开mysql手册,在 这里 找到这一段:

mysql_insert_id() returns the value stored into an AUTO_INCREMENT column, whether that value is automatically generated by storing NULL or 0 or was specified as an explicit value. LAST_INSERT_ID() returns only automatically generated AUTO_INCREMENT values. If you store an explicit value other than NULL or 0, it does not affect the value returned by LAST_INSERT_ID().

结论

从手册的描述可以知道, mysql_insert_id 函数返回的是储存在有 AUTO_INCREMENT 约束的字段的值,如果表中的字段不使用 AUTO_INCREMENT 约束或者使用自己生成的唯一值插入,那么该函数不会返回你所存储的值,而是返回NULL或0。因此,在没有使用AUTO_INCREMENT约束的表中,或者ID是自己生成的唯一ID,lastInsertId函数返回的都是0。

本文是探讨一个问题出现的原因,由于个人水平有限,如有建议和批评,欢迎指出。

注:本文使用的是PHP5.4.15,MySQL5.5.41。

人气教程排行