PDO :: exec
PDO :: exec - execute a SQL statement and returns the number of affected rows (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)
Explanation
grammar
int PDO::exec ( string $statement )
PDO :: exec () to execute an SQL statement in a single function call, returning the number of rows affected by the statement.
PDO :: exec () will not return results from a SELECT statement. For the program only need to issue a SELECT statement, consider using PDO :: query ().
Parameter Description:
statement: SQL statement to be pretreated and implementation.
return value
PDO :: exec () returns the number of rows affected by the impact of deleting or modifying the SQL statement. If no affected rows PDO :: exec () returns 0.
The following example relies PDO :: exec () return value is incorrect, the affected lines as 0 statement will result in a call die ():
<?php $db->exec() or die(print_r($db->errorInfo(), true)); ?>
Examples
Execute a DELETE statement
Calculated by the number of rows in a DELETE statement with no WHERE clause is deleted.
<?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.