DBI Query - prepare and execute

Prepare

Before sql statement can be executed, it must be prepared through prepare() function. It returns statement handle.

Example:

$sth = $dbh->prepare("SELECT * FROM user where user LIKE ?");

 

Bind Parameters

We have to specify parameter no. and bind value.

Example :

$rv = $sth->bind_param($p_num, $bind_value);

or

$rv = $sth->bind_param($p_num, $bind_value, $bind_type);

e.g.
$rv = $sth->bind_param(1, "root");

Execute

You can execute sql statement once it has been prepared & parameter has been bound using execute function.

Example:

rvalue = $sth->execute(); # bind is already done.

or 

rvalue = $sth->execute("root"); # in case we didn't bind earlier.


Finish

To release the resources, once we are done with statement handle.

Example:

$sth->finish();

 

Do

The do() function combines prepare() and execute() for non-SELECT statement.
It is invoked directly on db connection handle.

Example :

$rows_affected = $dbh->do("UPDATE bar SET foo = foo + 1");

Last modified: Monday, 2 April 2012, 02:25 AM

Skip Navigation

Navigation

Skip OpenGurukulSkip Settings

Settings