Friday, July 13, 2012

What is the difference between do() & prepare() ?


The do() method usually creates, prepares, executes and destroys a statement handle each time. It can be used for non repeated non-SELECT statement (or with drivers that don't support placeholders). For example, if you're doing an UPDATE, INSERT or DELETE there are no data that come back from the database. So you can say:

$dbh->do('DELETE FROM people WHERE age > 65');

do() returns either a true or false value. To be precise, it returns the number of affected rows on success.

On the contrary, the prepare( ) method will almost inevitably result in better performance, particularly on databases which do actually cache the statement. Always replace do( ) with prepare( ) and execute( ) in a loop to attain best performance.

No comments :