大网数据广告活动
PHP 语言与运行手册

mysqli_stmt::$insert_id · Returns the value generated for an AUTO_INCREMENT column by the last statement executed

· 52CE 我爱测

mysqli_stmt::$insert_id -- mysqli_stmt_insert_id — Returns the value generated for an AUTO_INCREMENT column by the last statement executed

mysqli_stmt::$insert_id

mysqli_stmt_insert_id

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$insert_id -- mysqli_stmt_insert_id — Returns the value generated for an AUTO_INCREMENT column by the last statement executed

说明

面向对象风格

过程化风格

function mysqli_stmt_insert_id(mysqli_stmt $statement): int|string

Returns the ID generated by an INSERT or UPDATE statement on a table with a column having the AUTO_INCREMENT attribute. In the case of a multiple-row INSERT statement, it returns the first automatically generated value that was successfully inserted. If LAST_INSERT_ID(expr) is used to set the AUTO_INCREMENT value, or in an UPDATE, the value of expr is returned instead.

Returns 0 if the statement did not change an AUTO_INCREMENT value.

参数

statement

仅以过程化样式:由 mysqli_stmt_init() 返回的 mysqli_stmt 对象。

返回值

The value of the AUTO_INCREMENT field that was updated by the statement. Returns zero if the statement has not been executed yet or if it did not update an AUTO_INCREMENT value.

The value is specific to the statement object. It is not affected by other statements executed on the same connection after this statement, nor by statements issued using other connections or clients.

注意:

If the number is greater than the maximum int value, it will be returned as a string.

示例

示例 #1 mysqli_stmt::$insert_id example

面向对象风格

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");

$stmt = $mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District, Population) VALUES (?, ?, ?, ?)");

$name = 'Stuttgart';
$countryCode = 'DEU';
$district = 'Stuttgart';
$population = 617000;
$stmt->bind_param('sssi', $name, $countryCode, $district, $population);
$stmt->execute();

printf("New record has ID %d.\n", $stmt->insert_id);

过程化风格

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");

$stmt = mysqli_prepare($link, "INSERT INTO myCity (Name, CountryCode, District, Population) VALUES (?, ?, ?, ?)");

$name = 'Stuttgart';
$countryCode = 'DEU';
$district = 'Stuttgart';
$population = 617000;
mysqli_stmt_bind_param($stmt, 'sssi', $name, $countryCode, $district, $population);
mysqli_stmt_execute($stmt);

printf("New record has ID %d.\n", mysqli_stmt_insert_id($stmt));

以上示例会输出:

New record has ID 1.

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。