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:

<?php
$dbh = new PDO('odbc:sample','db2inst1','ibmdb2');
/* Delete all rows from the FRUIT table where colour is 'red' */
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
/* Output the number of deleted rows */
print("Deleted $count rows.
");
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendSQLdatabasePHPPDO
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

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.