Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PDO::exec

PDO::exec

PHP PDO Reference ManualPHP PDO Reference Manual

PDO::exec - Execute an SQL statement and return 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 SELECT statements that need to be issued only once in a program, consider using PDO::query().

Parameter Description:

statement: The SQL statement to be prepared and executed.


Return Value

PDO::exec() returns the number of rows affected by the modified or deleted SQL statement. If no rows were affected, PDO::exec() returns 0.

The following example incorrectly relies on the return value of PDO::exec(), where a statement with 0 affected rows would cause die() to be called:



exec() or die(print_r($db->errorInfo(), true));

?>

Example

Execute a DELETE statement

Count the rows deleted by a DELETE statement without a WHERE clause.



exec("DELETE FROM fruit WHERE colour = 'red'");



/* Return the number of deleted rows */

print("Deleted $count rows.\n");

?>

The above example will output:



Deleted 1 rows.


PHP PDO Reference ManualPHP PDO Reference Manual