get()

The get() method builds and executes a complete SELECT query using all previously defined clauses. It always returns a full result set, even when only one row matches. Additional methods like setReturnKey() and setOutputMode() allow full control over the structure of the result.

Signature:

public function get(string $table, int|array|null $limit = null, string|array $columns = '*'): array|false

Basic Usage

$users = $db->get('users');

With Limit

$latest = $db->get('orders', 10);

With Offset and Limit

You can pass an array as the second argument to apply both offset and limit:

$paged = $db->get('orders', [40, 20]); // returns 20 rows starting from offset 40

Select Specific Columns

$data = $db->get('users', null, ['id', 'email']);

Keying results using setReturnKey()

If you want to use a specific column (e.g. id) as array key, call setReturnKey() before get():

$db->setReturnKey('id');
$orders = $db->get('orders', 2);

/*
[
  9 => [
    'id' => 9,
    'user_id' => 9,
    'product_name' => 'Tinfoil Hat Pack (3x)',
    'price' => 8.88,
    'status' => 'completed',
    ...
  ],
  42 => [
    'id' => 42,
    'user_id' => 42,
    'product_name' => 'Tinfoil Hat Pack (3x)',
    'price' => 8.88,
    'status' => 'canceled',
    ...
  ]
]
*/

Change Output Format using setOutputMode()

The default output is an array of arrays. You can change the format to return objects instead:

$db->setOutputMode(PDO::FETCH_OBJ);
$orders = $db->get('orders', 2);

/*
[
  0 => (object) { id: 1, total: 99.00, ... },
  1 => (object) { id: 2, total: 42.50, ... }
]
*/

If you want a flat single result instead, use getOne().

Total Count Support

When using pagination or limits, call withTotalCount() before get() to retrieve the total number of matching rows:

  • $db->count — Number of returned rows
  • $db->totalCount — Total matching rows without LIMIT

Security Notes

  • Table names are strictly validated against injection.
  • Always use where() or safe variants like whereInt(), whereString() before get().
  • JOINs, subqueries, GROUP BY and HAVING are fully supported.

See Also