YouTip LogoYouTip

Pdo Exec

# PDO::exec [![Image 3: PHP PDO Reference](#)PHP PDO Reference](#) 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 that statement. PDO::exec() does not return results from a SELECT statement. For a SELECT statement that you only need to issue once in your 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 modifying or deleting SQL statement. If no rows are affected, PDO::exec() returns 0. The following example incorrectly relies on the return value of PDO::exec(), where a statement affecting 0 rows would cause die() to be called: exec() or die(print_r($db->errorInfo(), true));?> ## Example ### Executing a DELETE statement Count the number of 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](#)
← Bootstrap IntroPdo Errorinfo β†’