Runo

Runo — Raw SQL & results

Use DB::prepare() to create a Statement, bind values with args(), optionally cast/hydrate, then call execute() to get a Result.

Raw queries

use Runo\DB;

$result = DB::prepare(
    'SELECT id, title, status FROM posts WHERE status = :status ORDER BY id DESC'
)->args([
    ':status' => 'published',
])->execute();

foreach ($result as $post) {
    echo $post->title . PHP_EOL;
}

Binding values

Use named placeholders with leading colons. PDO types are inferred from PHP values:

PHP valuePDO parameter type
intPDO::PARAM_INT
boolPDO::PARAM_BOOL
nullPDO::PARAM_NULL
floats/strings/othersPDO::PARAM_STR

Inserting rows

$result = DB::prepare(
    'INSERT INTO posts (title, status) VALUES (:title, :status)'
)->args([
    ':title'  => 'Hello from Runo',
    ':status' => 'draft',
])->execute();

$id = $result->meta()->id;
$affected = $result->meta()->affected;

meta()->id is populated only for statements starting with INSERT (from PDO::lastInsertId(), cast to int). The statement also exposes $statement->id.

Updating rows

$result = DB::prepare(
    'UPDATE posts SET title = :title WHERE id = :id'
)->args([
    ':title' => 'Updated title',
    ':id'    => 1,
])->execute();

echo 'Updated rows: ' . $result->meta()->affected;

Deleting rows

$result = DB::prepare(
    'DELETE FROM posts WHERE id = :id'
)->args([':id' => 1])->execute();

echo 'Deleted rows: ' . $result->meta()->affected;

Fetch behavior

Statement::execute() fetches all rows when the statement reports at least one column (columnCount() > 0) via fetchAll(PDO::FETCH_ASSOC). This covers SELECT, SHOW, DESCRIBE, EXPLAIN, etc. Results are eager (no lazy cursor).

Working with results

Result implements IteratorAggregate and Countable.

$result = DB::prepare('SELECT id, title FROM posts')->execute();

foreach ($result as $post) { echo $post->title . PHP_EOL; }
echo count($result);
// echo $result->count();

$post = $result->first();
if ($post !== null) { echo $post->title; }

$ids = $result->pluck('id');      // values via object access
$titles = $result->pluck('title');

$records = $result->toArray();    // array of Record objects (not scalar arrays)

$rows = array_map(fn ($r) => $r->toArray(), $result->toArray());
echo json_encode($rows, JSON_THROW_ON_ERROR);

Result and Record are not JsonSerializable; convert with toArray() before json_encode().

Query metadata

$meta = $result->meta();

echo $meta->id;       // last insert ID (INSERT only; else 0)
echo $meta->affected; // affected/returned row count
echo $meta->time;     // execution time in seconds

Part of the Lunaris documentation · Source on Codeberg · MIT License

Part of the Lunaris documentation · Source on Codeberg · MIT License