Introduction - Query -- Performing queries
Description
PEAR DB provides several methods for querying databases.
The most direct method is
query(). It takes a SQL query
string as an argument.
There are three possible returns:
a new DB_result object
for queries that return results (such as
SELECT queries),
DB_OK for queries that manipulate data (such as
INSERT queries)
or a DB_Error object on failure.
Example 33-1. Doing a query <?php
// Create a valid DB object named $db
// at the beginning of your program...
require_once 'DB.php';
$db =& DB::connect('pgsql://usr:pw@localhost/dbnam');
if (PEAR::isError($db)) {
die($db->getMessage());
}
// Proceed with a query...
$res =& $db->query('SELECT * FROM clients');
// Always check that result is not an error
if (PEAR::isError($res)) {
die($res->getMessage());
}
?> |
|
query() can be used instead of
prepare() and
execute(), if you
set the $params parameter
and your query uses placeholders.