In daily development, data deletion is a very common but requires careful processing task. Especially in scenarios involving large-scale data processing or requiring execution efficiency, it is particularly important to accurately determine whether the deletion operation is successful and whether it is necessary to continue to execute subsequent logic. At this time, the PDOStatement::rowCount function can play its role.
PDOStatement::rowCount is a method after PDO executes SQL in PHP, which is used to return the number of rows affected by the previous SQL statement . For statements such as DELETE , UPDATE , INSERT , etc., it can tell us exactly how many rows of data are actually changed or deleted.
$pdo = new PDO("mysql:host=localhost;dbname=testdb", "user", "password");
$stmt = $pdo->prepare("DELETE FROM users WHERE last_login < :threshold");
$stmt->execute([':threshold' => '2024-01-01']);
$deletedRows = $stmt->rowCount();
echo "Deleted in total $deletedRows Line records。";
In this example, if there are records that meet the criteria in the user table, rowCount() can return how many rows have been deleted.
Many times, we may unconditionally execute a delete statement in program logic, but in fact, there may not be records that meet the conditions in the database. Although this meaningless deletion operation will not report an error, it increases the burden on the database.
Use rowCount() to accurately determine whether to execute subsequent logic before and after execution:
$stmt = $pdo->prepare("DELETE FROM logs WHERE created_at < :expired_time");
$stmt->execute([':expired_time' => date('Y-m-d H:i:s', strtotime('-30 days'))]);
if ($stmt->rowCount() > 0) {
// Records have been deleted,Can perform subsequent cleaning tasks
file_get_contents("https://gitbox.net/cleanup-notify");
}
By determining whether a record is actually deleted, unnecessary remote calls or other resource consumption is avoided.
When handling large batch deletion operations, two database accesses may be required if you first use SELECT COUNT(*) checks. rowCount() can immediately give the number of rows affected after execution, saving a query overhead.
$stmt = $pdo->prepare("DELETE FROM temp_data WHERE expire_at < NOW()");
$stmt->execute();
if ($stmt->rowCount() === 0) {
echo "No data to be deleted。";
} else {
echo "Deleted successfully {$stmt->rowCount()} Temporary data。";
}
This method is especially suitable for writing in timed cleaning scripts to avoid unnecessary repetition of work.
When performing database operations, recording the number of affected rows is critical for troubleshooting and performance monitoring. The return value of rowCount() can be used to build detailed logs to help you locate performance bottlenecks or logical errors.
$logMessage = sprintf(
"[%s] Cleaning operation is completed,Delete in total %d Expired order。",
date('Y-m-d H:i:s'),
$stmt->rowCount()
);
error_log($logMessage, 3, '/var/log/cleanup.log');
While rowCount() performs well in most databases, it doesn't always work for SELECT statements. In some database drivers, calling rowCount() on a SELECT statement will return 0, so it should only be used for write operations such as INSERT , DELETE , and UPDATE .