PEAR Manual
Prev |  | Next

# DB_common::limitQuery()

DB_common::limitQuery() -- Sends a LIMIT query to the database

## Synopsis

``mixed &limitQuery` (string $query, integer $from, integer $count [, mixed $params = array()])`

## Description

Executes a SQL query, but fetches only the the specificed count of rows. It is an emulation of the MySQL LIMIT option.

## Parameter

string `$query`

  the SQL query

integer `$from`

  the row to start to fetch. Note that 0 returns the first row, 1 returns the second row, etc.

integer `$count`

  the numbers of rows to fetch

mixed `$params`

  array, string or numeric data to be added to the prepared statement. Quantity of items passed must match quantity of placeholders in the prepared statement: meaning 1 placeholder for non-array parameters or 1 placeholder per array element.

## Return value

mixed - 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

## Throws

Table 33-1. Possible PEAR_Error values

Error code | Error message | Reason | Solution
--- | --- | --- | ---
`DB_ERROR_NODBSELECTED` | no database selected | No database was choosen. | Check the DSN in connect().
every other error code |  | Database specific error | Check the database related section of PHP-Manual to detect the reason for this error. In the most cases a misformed SQL statment. Ie. using LIMIT in a SQL-Statment for an Oracle database.

## Note

This function can not be called statically.

Depending on the database you will not really get more speed compared to query(). The advantage of limitQuery() is the deleting of unneeded rows in the resultset, as early as possible. So this can decrease memory usage.

## Example

Example 33-1. Using limitQuery()
    <?php
    // Once you have a valid DB object named $db...
    $res =& $db->limitQuery('SELECT * FROM foo', 49, 10);

    if (PEAR::isError($res)) {
        die($res->getMessage());
    }
    ?>

## See

query()

Prev | Home | Next
DB_common::getRow() | Up | DB_common::nextId()