PHP mysqli_insert_id() 函数

PHP 教程 · 2019-01-28 08:13:26

假设 websites 表有一个自动生成的 ID 字段。返回最后一次查询中的 ID:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:CODERCTO 
$con=mysqli_connect("localhost","root","123456","CODERCTO"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

mysqli_query($con,"INSERT INTO websites (name,url,alexa) 
VALUES ('码农教程','http://www.codercto.com',5633)");

// 输出自动生成的 ID
echo "新 id 为: " . mysqli_insert_id($con); 

mysqli_close($con);
?>

定义和用法

mysqli_insert_id() 函数返回最后一个查询中自动生成的 ID(通过 AUTO_INCREMENT 生成)。

语法

mysqli_insert_id(connection);

参数 描述
connection 必需。规定要使用的 MySQL 连接。

技术细节

返回值: 返回一个在最后一个查询中自动生成的带有 AUTO_INCREMENT 字段值的整数。如果数字 > 最大整数值,它将返回一个字符串。如果没有更新或没有 AUTO_INCREMENT 字段,将返回 0。
PHP 版本: 5+

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

Measure What Matters

Measure What Matters

John Doerr / Portfolio / 2018-4-24 / GBP 19.67

In the fall of 1999, John Doerr met with the founders of a start-up he’d just given $11.8 million, the biggest investment of his career. Larry Page and Sergey Brin had amazing technology, entrepreneur......一起来看看 《Measure What Matters》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具