Neevo

Tiny database layer for PHP

Documentation – Data retrieval

We have already mentioned a way to retrieve data from database – using foreach() loop. But there are more ways to retrieve data in Neevo.

Neevo\Result, a class which is instantiated when you call $neevo->select(), provides many useful methods.

Iteration

Once again, you can fetch all rows by simply iterating through Neevo\Result, since it implements the IteratorAggregate interface and uses Neevo\ResultI­terator for iterating. This iterator is also seekable and countable.

fetch()

This is the basic method for retrieving from database. It returns the current row from the result and also moves internal database cursor to the next row, so when you call it again, you get the following row.

$result = $neevo->select('users');

$row_1 = $result->fetch();
$row_2 = $result->fetch();

fetchAll()

As you may have guessed from the name of this method, it returns all rows of the result as an array of rows. You can also set $limit and $offset arguments to limit returned rows.

// SELECT * FROM `users` LIMIT 15 OFFSET 5;
$result = $neevo->select('users')->limit(15, 5);

// Get rows 5-20 (Limited by SQL statement)
$rows = $result->fetchAll();

// Get rows 10-15 (Limited by SQL statement and fetchAll() definition)
$rows = $result->fetchAll(5, 5) // Limit: 5, Offset: 5

fetchPairs()

This is a very useful method. Let's see example code first:

$pairs = $neevo->select('users')->fetchPairs('id', 'name');

foreach($pairs as $id => $name){
    echo "#$id: $name";
}

This method returns an associative array with first specified field as a key and second one as a value.

You can also omit the second argument – then an associative array of whole rows is returned, with specified field as a key to the rows.

fetchSingle()

This method does the same as fetch() but returns only the first value of row.

Seek and count

With seek(), you can seek to specified offset of the result.

Since Neevo\Result implements Countable interface, you can simply call count($result) (or $result->count() if you prefer this way) to get the number of rows in a result set.

However, both these methods cannot be used on unbuffered results and they throw a Neevo\Exception in that case.

With count(), you can also specify a table field to count:

// SELECT COUNT(*) FROM `users`;
$neevo->select('users')->count('*');

Aggregation functions

This is another feature of Neevo that you can find useful. With aggregation() method, you can perform any aggregation functions on the current result set. There are also some predefined ones as individual methods: min(), max(), sum() and already mentioned count().

Debugging tools

For debugging purposes, there are two useful methods.

dump()

This one is also available for INSERT, UPDATE and DELETE statements. As you can guess, it dumps a syntax-highlighted query instead of performing it.

$neevo->select('users')->where('name', 'Albert Einstein')->dump();

Will output:

SELECT * 
FROM users
WHERE (name = ‚Albert Einstein‘)

explain()

This is available only for SELECT statements. It returns an associative array of rows returned by running EXPLAIN on the SELECT statement.

« Basic manipulation Mastering Neevo »

Fork me on GitHub