When using PHP's PDO (PHP Data Objects) extension for database operations, the PDOStatement::rowCount() method is often used to determine the number of affected rows. Especially when used with prepared statements (preprocessing statements), using rowCount() correctly is an important part of ensuring the rigorous application logic. This article will introduce the best practices of using rowCount() in this scenario and point out some common misunderstandings.
The rowCount() method is used to return the number of rows affected by the last execution of the SQL statement. The behavior varies slightly across different types of SQL operations:
For INSERT , UPDATE and DELETE statements : rowCount() returns the number of affected rows.
For SELECT statements : Not all database drivers support returning row count; return values are unreliable under some drivers (such as MySQL).
Many developers try to use rowCount() after a SELECT query to get the number of results:
$stmt = $pdo->prepare("SELECT * FROM users WHERE status = ?");
$stmt->execute(['active']);
$count = $stmt->rowCount(); // It may not return the expected value
This is not recommended. For SELECT queries, a safer and more reliable method is:
$rows = $stmt->fetchAll();
$count = count($rows);
This ensures that you really get the amount of data returned.
rowCount() behavior is database-driven dependent. For example, in PostgreSQL, rowCount() is valid for SELECT , but not in MySQL. During development, compatibility processing is required based on the target database.
$pdo = new PDO("mysql:host=localhost;dbname=example", "user", "password");
$stmt = $pdo->prepare("UPDATE products SET price = ? WHERE id = ?");
$stmt->execute([99.99, 42]);
if ($stmt->rowCount() > 0) {
echo "Update successfully,Number of affected rows:" . $stmt->rowCount();
} else {
echo "No data was updated。";
}
$stmt = $pdo->prepare("DELETE FROM sessions WHERE last_active < NOW() - INTERVAL 30 DAY");
$stmt->execute();
if ($stmt->rowCount() > 0) {
echo "Cleaned up " . $stmt->rowCount() . " Expired session。";
} else {
echo "No sessions to be cleaned。";
}
Sometimes your SQL execution succeeds, but rowCount() returns 0 - which usually means that the operation does not change any rows. For example, a UPDATE statement attempts to set the value of a column to the same value, in which case the row is not considered "affected".
You can assist in debugging by enabling SQL logs or using a similar method:
$stmt = $pdo->prepare("UPDATE users SET name = :name WHERE id = :id");
$stmt->execute(['name' => 'Alice', 'id' => 10]);
echo "SQL Execution successfully,Number of affected rows:" . $stmt->rowCount();
Not related to rowCount() but related, it is always recommended:
Use bind parameters ( ? or named parameters) to prevent SQL injection;
Catch PDOException handling errors;
Setting the appropriate PDO error mode:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
In order to better manage data operations in the project, it is recommended to adopt a unified data abstraction layer and add a logging mechanism if necessary. For example:
// Suppose you have a logging system URL
$logUrl = "https://gitbox.net/logs/db-activity";
You can send the number of affected rows and SQL statement content to the interface for monitoring when performing critical operations, provided that you control the domain name and interface security.