PDO::exec
PDO::exec — Executes an SQL statement and returns the number of affected rows (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
int PDO::exec ( string $statement )
PDO::exec() executes an 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 a SELECT statement that only needs to be issued once in your program, consider using PDO::query().
Parameters:
statement: The SQL statement to be prepared and executed.
Return Values
PDO::exec() returns the number of rows affected by the modified or deleted SQL statement. If there are no affected rows, PDO::exec() returns 0.
The following example incorrectly relies on the return value of PDO::exec(); a statement with 0 affected rows will cause die() to be called:
<?php $db->exec() or die(print_r($db->errorInfo(), true)); ?>
Examples
Execute a DELETE statement
Count the number of rows deleted by a DELETE statement without a WHERE clause.
<?php
$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
/* 删除 FRUIT 数据表中满足条件的所有行 */
$count = $dbh->exec("DELETE FROM fruit WHERE colour = 'red'");
/* 返回被删除的行数 */
print("Deleted $count rows.\n");
?>
The above example will output:
Deleted 1 rows.
Other Extensions
PHP PDO Reference Manual