Database Abstraction with PEAR - DB::getOne and DB::query()
(Page 6 of 7 )
mixed &getOne (string $query [, array $params]) |
Now, this is a useful little function. This allows you to grab the first column in the first row of data that a query returns. It is very useful for doing count functions or sum functions. It returns the value of the first column in the first row or an error object.
A typical use would be:
<?php $numrows = $db->getOne('SELECT count(*) FROM mystuff'); ?> |
One nice thing about DB::getone is that it does the query, gets the results, and frees the results. No need to worry about cleanup with this function.
mixed &query (string $query [, array $params]) |
This is the general purpose query function. On failure it will return an error object. On success it will return either a DB_OK or a result set object. A DB_OK is a PEAR constant that just tells you the query executed successfully. You will receive a DB_OK for any query that executes properly that doesn't return a result set, i.e. an INSERT or UPDATE.
Usage for this function:
<?php $sql = "SELECT * FROM mystuff ORDER BY stuff"; $result = $db->query($sql); ?> |
Next: DB_Result::fetchRow >>
More Database Articles Articles
More By Matt Wade