Current Location: Home> Latest Articles> How to check the number of rows affected by a query using PDOStatement::rowCount

How to check the number of rows affected by a query using PDOStatement::rowCount

gitbox 2025-05-28

In PHP, when using PDO (PHP Data Objects) to perform database operations, it is often necessary to check the number of rows affected by a query statement, especially when performing updates, deletions, or insertions. PDO provides a very useful function PDOStatement::rowCount() , which can help developers get the number of rows affected by the operation of the most recent SQL statement. This article will explain in detail how to use PDOStatement::rowCount() to check the number of rows affected by database query operations.

1. The role of PDOStatement::rowCount

The PDOStatement::rowCount() function returns the number of rows affected by the last database operation. It is usually used for the following operations:

  • When executing INSERT , UPDATE , or DELETE statements, check the number of records affected by the operation.

  • Used to determine whether a database operation is successful, such as how many rows of data are updated or how many records are deleted.

2. Use rowCount to query operations

PDOStatement::rowCount() is mainly used to query the number of rows affected after execution. We demonstrate how to use it with the following sample code.

3. Sample code

Suppose we have a simple database operation, using PDO to execute a UPDATE statement and get the updated number of rows.

 <?php
// Connect to the database
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'root';
$password = '';
$options = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
);

try {
    $pdo = new PDO($dsn, $username, $password, $options);
    echo "Connection successfully!<br>";

    // Update statement
    $sql = "UPDATE users SET status = 'active' WHERE age > 30";
    $stmt = $pdo->prepare($sql);

    // Perform update operations
    $stmt->execute();

    // Get the number of rows affected
    $rowCount = $stmt->rowCount();
    echo "Number of rows affected: " . $rowCount;
} catch (PDOException $e) {
    echo "Database connection failed: " . $e->getMessage();
}
?>

In this example:

  1. We first connect to the database.

  2. Use the UPDATE statement to update the record of age > 30 in the users table.

  3. After executing the update statement, call rowCount() to get the actual number of rows updated.

  4. The number of rows affected by the last output.

If the execution is successful, rowCount() returns the number of records affected by the update operation.

4. Things to note

  • For SELECT queries, rowCount() returns the number of rows affected by the database. However, the MySQL database does not necessarily support this feature for SELECT statements, it usually returns 0 or NULL. Therefore, rowCount() can accurately return the number of rows affected only when performing operations such as INSERT , UPDATE , DELETE , etc.

  • For some database management systems, such as PostgreSQL, rowCount() returns the actual number of rows retrieved when executing a SELECT query.

5. Use rowCount() to delete

Similarly, you can use rowCount() to check the number of rows affected by the delete operation. For example:

 <?php
// Delete the user status as 'inactive' Records of
$sql = "DELETE FROM users WHERE status = 'inactive'";
$stmt = $pdo->prepare($sql);

// Perform a delete operation
$stmt->execute();

// Get the number of deleted rows
$rowCount = $stmt->rowCount();
echo "Number of rows deleted: " . $rowCount;
?>

In this example, rowCount() returns the number of records that have been deleted. If no record meets the criteria, return 0 .

6. Conclusion

PDOStatement::rowCount() is a very useful function that helps developers easily get the number of rows affected after executing SQL statements. This is very important for understanding the operation effect and handling business logic after performing updates, deletions and other operations. By rationally utilizing this function, database operations can be performed more effectively and the readability and reliability of the code can be enhanced.

If you want to gain insight into other features of PDO, you can access relevant documentation or tutorials to refer to more practical cases and best practices.