PHP database update operation: How to use the mysqli_affected_rows function to get the number of affected rows
In PHP development, interacting with the database is a common task. Among them, the update operation is a frequently used operation, mainly used to modify the data in the database. After we perform an update operation, sometimes we need to know how many rows of data were actually affected. At this time, you can use the mysqli_affected_rows function provided by PHP to get the number of affected rows.
mysqli_affected_rows is a function in PHP used to get the number of rows affected by the last MySQL operation. It returns the number of rows affected after the latest INSERT, UPDATE, and DELETE statements were executed. This function only applies to connecting to MySQL database using the mysqli extension library.
(1) Connect to the MySQL database
Before using the mysqli_affected_rows function, you first need to connect to the MySQL database. You can use the mysqli_connect function to establish a connection to a MySQL database. The following is a sample code:
$hostname = "localhost"; // MySQL服务器地址 $username = "root"; // MySQL用户名 $password = "123456"; // MySQL密码 $database = "test"; // 数据库名 // 创建连接 $conn = mysqli_connect($hostname, $username, $password, $database); if (!$conn) { die("连接失败: " . mysqli_connect_error()); }
(2) Perform the update operation
Next, use the mysqli_query function to perform the update operation. The following is a sample code for modifying the value of a field in the table:
$sql = "UPDATE users SET age = 25 WHERE id = 1"; if (mysqli_query($conn, $sql)) { echo "更新成功"; } else { echo "更新失败: " . mysqli_error($conn); }
(3) Get the number of affected rows
After the update operation is executed, you can use the mysqli_affected_rows function Get the number of affected rows. The following is a sample code:
$affected_rows = mysqli_affected_rows($conn); echo "受影响的行数:" . $affected_rows;
The following is a complete sample code that shows how to use the mysqli_affected_rows function to get the number of affected rows:
$hostname = "localhost"; $username = "root"; $password = "123456"; $database = "test"; // 创建连接 $conn = mysqli_connect($hostname, $username, $password, $database); if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 执行更新操作 $sql = "UPDATE users SET age = 25 WHERE id = 1"; if (mysqli_query($conn, $sql)) { echo "更新成功"; } else { echo "更新失败: " . mysqli_error($conn); } // 获取受影响的行数 $affected_rows = mysqli_affected_rows($conn); echo "受影响的行数:" . $affected_rows; // 关闭连接 mysqli_close($conn);
It can be clearly seen from the above code that using the mysqli_affected_rows function can easily obtain the number of rows affected by the update operation and perform subsequent processing.
Summary: In PHP development, if you need to get the number of rows affected by an update operation, you can use the mysqli_affected_rows function. First connect to the MySQL database, then perform the update operation, and finally get the number of affected rows through the mysqli_affected_rows function. This operation is very simple and saves us time and energy in writing redundant code. I hope this article is helpful for you to understand how to use the mysqli_affected_rows function.
The above is the detailed content of PHP database update operation: How to get the number of affected rows using the mysqli_affected_rows function. For more information, please follow other related articles on the PHP Chinese website!