PDO::exec — Execute a SQL statement and return the number of affected rows (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
int PDO::exec ( string $statement )
PDO::exec() executes a SQL statement in a single function call, returning the number of rows affected by the statement.
PDO::exec() does not return results from a SELECT statement. For SELECT statements that only need to be issued once in the program, consider using PDO::query().
statement : The SQL statement to be prepared and executed.
PDO::exec() Returns the number of rows affected by a modify or delete SQL statement. If there are no affected rows, PDO::exec() returns 0.
The following example relies incorrectly on the return value of PDO::exec(), where a statement with 0 affected rows would cause die() to be called:
<?php$db->exec() or die(print_r($db->errorInfo(), true));?>
Counts the number of rows deleted by a DELETE statement without a WHERE clause.
<?php$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');/* Delete all rows that meet the conditions in the FRUIT data table */$count = $dbh->exec("DELETE FROM fruit WHERE color = 'red'");/* Return the number of deleted rows*/print("Deleted $count rows.n");?>
The above routine will output:
Deleted 1 rows.