PHP - Manual: MysqlndUhConnection::getLastInsertId
2024-12-20
(PECL mysqlnd-uh >= 1.0.0-alpha)
MysqlndUhConnection::getLastInsertId — Returns the auto generated id used in the last query
$connection
) : intReturns the auto generated id used in the last query.
connection
Mysqlnd connection handle. Do not modify!
Last insert id.
Example #1 MysqlndUhConnection::getLastInsertId() example
<?php
class proxy extends MysqlndUhConnection {
public function getLastInsertId($res) {
printf("%s(%s)\n", __METHOD__, var_export(func_get_args(), true));
$ret = parent::getLastInsertId($res);
printf("%s returns %s\n", __METHOD__, var_export($ret, true));
return $ret;
}
}
mysqlnd_uh_set_connection_proxy(new proxy());
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query("DROP TABLE IF EXISTS test");
$mysqli->query("CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, col VARCHAR(255))");
$mysqli->query("INSERT INTO test(col) VALUES ('a')");
var_dump($mysqli->insert_id);
?>
以上例程会输出:
proxy::getLastInsertId(array ( 0 => NULL, )) proxy::getLastInsertId returns 1 int(1)
官方地址:https://www.php.net/manual/en/mysqlnduhconnection.getlastinsertid.php