PDO exec

PDO::exec() — Execute an SQL statement and return the number of affected rows

  1. PDO::exec() executes an SQL statement in a single function call, returning the number of rows affected by the statement.
  2. PDO::exec() does not return results from a SELECT statement.
  3. For a SELECT statement that you only need to issue once during your program, consider issuing PDO::query().
  4. For a statement that you need to issue multiple times, prepare a PDOStatement object with PDO::prepare() and issue the statement with PDOStatement::execute().
  5. PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec() returns 0.

Example #1 Issuing a DELETE statement

<?php

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

/* Delete all rows from the FRUIT table */
$count = $db->exec("DELETE FROM fruit WHERE colour = 'red'");

/* Return number of rows that were deleted */

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

Loading ...

Related Results :

  1. PDO exec
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author