Backend Development 2 min read

PDO::exec – Execute an SQL Statement and Return the Number of Affected Rows

The article explains PHP's PDO::exec method, detailing its signature, parameters, return value, and provides a complete example showing how to delete rows from a database table and retrieve the count of affected rows.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PDO::exec – Execute an SQL Statement and Return the Number of Affected Rows

PDO::exec is a PHP method that executes a given SQL statement in a single function call and returns the number of rows affected by the operation.

Signature: int PDO::exec(string $statement)

Parameter: $statement – the SQL statement to be prepared and executed.

Return value: The number of rows modified or deleted by the SQL statement; if no rows are affected, PDO::exec returns 0.

Example:

exec("DELETE FROM fruit WHERE colour = 'red'");
/* Output the number of deleted rows */
print("Deleted $count rows.\n");
?>
SQLDatabasePDO
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.