10.6.1. ProblemYou want to add, remove, or change data in an SQL database. 10.6.2. SolutionUse PDO::exec( ) to send an INSERT, DELETE, or UPDATE command, as shown in Example 10-14. Using PDO::exec( )
You can also prepare a query with PDO::prepare( ) and execute it with PDOStatement::execute( ), as shown in Example 10-15. Preparing and executing a query
10.6.3. DiscussionThe exec( ) method sends to the database whatever it's passed. For INSERT, UPDATE, and DELETE queries, it returns the number of rows affected by the query. The prepare( ) and execute( ) methods are especially useful for queries that you want to execute multiple times. Once you've prepared a query, you can execute it with new values without re-preparing it. Example 10-16 reuses the same prepared query three times. Reusing a prepared statement
10.6.4. See AlsoRecipe 10.7 for information on repeating queries; documentation on PDO::exec( ) at http://www.php.net/PDO::exec, on PDO::prepare( ) at http://www.php.net/PDO::prepare, and on PDOStatement::execute( ) at http://www.php.net/PDOStatement::execute. |