Home >Backend Development >PHP Tutorial >PHP database insertion operation: How to use the mysqli_insert_id function to obtain the auto-incrementing primary key value

PHP database insertion operation: How to use the mysqli_insert_id function to obtain the auto-incrementing primary key value

PHPz
PHPzOriginal
2023-08-02 12:09:201477browse

PHP database insertion operation: How to use the mysqli_insert_id function to obtain the auto-incrementing primary key value

In PHP development, interaction with the database is a very common operation. Among them, inserting data is the most commonly used operation. When inserting data, we often use auto-increment primary keys, and obtaining auto-increment primary key values ​​is a very important operation.

This article will teach you how to use the mysqli_insert_id function to obtain the auto-incrementing primary key value, helping you handle database operations more flexibly during development.

1. Create a database connection

Before performing a database insertion operation, you first need to establish a connection with the database. You can use the mysqli_connect function to create a connection to the database.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

// 创建数据库连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
?>

In the above code, the variables $servername, $username, $password, and $dbname respectively represent the server name, user name, password, and database name where the database is located. Replace it with your own database information.

2. Perform the data insertion operation

Next, we can use the mysqli_query function to perform the data insertion operation. It should be noted that when inserting data, a field in the table must be an auto-incrementing primary key, so that the relevant primary key value can be obtained.

<?php
// 执行插入数据的SQL语句
$sql = "INSERT INTO users (username, email) VALUES ('John Doe', 'johndoe@example.com')";

// 检查插入操作是否成功
if (mysqli_query($conn, $sql)) {
    $lastInsertedId = mysqli_insert_id($conn);
    echo "新记录插入成功,自增主键值为:" . $lastInsertedId;
} else {
    echo "插入失败:" . mysqli_error($conn);
}

// 关闭数据库连接
mysqli_close($conn);
?>

In the above code, we first executed a SQL statement to insert data and inserted a new record into the table named users. After the insertion is successful, we use the mysqli_insert_id function to obtain the auto-incrementing primary key value of the record and assign it to the $lastInsertedId variable.

3. Use of mysqli_insert_id function

The mysqli_insert_id function is used to obtain the value of the auto-incrementing primary key. It requires passing in a database connection object as a parameter. After calling this function, the auto-incremented primary key value of the last inserted record will be returned.

It should be noted that the mysqli_insert_id function must be called after the mysqli_query function is executed, otherwise the correct auto-incrementing primary key value will not be obtained.

4. Summary

The above is the method of using the mysqli_insert_id function to obtain the auto-incremented primary key value. When inserting data, this method can easily obtain the auto-incremented primary key value of the inserted record, making subsequent processing more flexible.

Whether in development or actual application, database insertion operation is one of the common operations. Mastering how to obtain auto-incrementing primary key values ​​can enable us to better utilize the features of the database and improve development efficiency and flexibility.

I hope this article will be helpful to your database insertion operations in PHP development. I wish you happy programming!

The above is the detailed content of PHP database insertion operation: How to use the mysqli_insert_id function to obtain the auto-incrementing primary key value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn