Database Reference

LavaLust includes a Database class that supports both semi-traditional SQL usage and a Query Builder pattern, allowing you to write safe and readable database code.

Database Configuration

Database connection values live in app/config/database.php.

<?php
$database['main'] = [
    'driver'   => 'mysqli',
    'hostname' => 'localhost',
    'port'     => '3306',
    'username' => 'root',
    'password' => '',
    'database' => 'my_database',
    'charset'  => 'utf8mb4',
    'dbprefix' => '',
];

Configuration Values

Key

Required

Description

driver

Yes

Database driver. See Supported Drivers below. Must be lowercase.

hostname

Yes

Hostname or IP address of the database server (e.g. localhost).

port

No

Port number. Defaults to the driver’s standard port when omitted.

username

Yes

Database user with appropriate privileges.

password

Yes

Password for the database user. Use an empty string for no password.

database

Yes

Name of the database to connect to.

charset

No

Connection character set. Recommended: utf8mb4 for full Unicode support.

dbprefix

No

Optional prefix prepended to every table name by the Query Builder. Useful when multiple applications share the same database.

path

yes

Required for SQLite connections. Specifies the full file path to the SQLite database file (for example: /var/www/database/app.db or C:/xampp/htdocs/app/database.db). Ignored by other database drivers.

Supported Drivers

Driver value

Database

Default port

Notes

mysql

MySQL / MariaDB

3306

Recommended for most applications. Requires the PHP mysqli extension.

postgre

PostgreSQL

5432

Requires the PHP pgsql extension.

sqlsrv

Microsoft SQL Server

1433

Requires the PHP sqlsrv extension (Microsoft SQL Server Driver for PHP).

sqlite

SQLite 3

n/a

File-based. Set hostname to the absolute path of the .db file. No username, password, or port is needed.

Connecting to Your Database

There are two ways to initialise the database class: auto-connect and manual connect.

Auto-connect

To load the database on every request, add 'database' to the libraries array in app/config/autoload.php:

<?php
$autoload['libraries'] = ['database'];

This connects using the default group and assigns the instance to $this->db.

Manual connect

When only some pages need database access, connect manually inside the controller or method that requires it:

<?php
// Connect using the default group → $this->db
$this->call->database();

// Connect using a named group → $this->group_name
$this->call->database('group_name');

Note

The property name on $this always matches the group name defined in database.php. The default group is always assigned to $this->db.

Connecting to Multiple Databases

You can open several connections in the same request:

<?php
$this->call->database();            // → $this->db        (default group)
$this->call->database('group_one'); // → $this->group_one
$this->call->database('group_two'); // → $this->group_two

After connecting, query each instance independently:

<?php
$this->db->table('users')->get_all();
$this->group_one->table('posts')->get_all();
$this->group_two->table('comments')->get_all();

Best Practices

Practice

Details

Keep SQL in models

Controllers should call model methods, never build queries directly.

Use the Query Builder

Values are bound automatically as prepared statement parameters, protecting against SQL injection.

Use bound parameters for raw SQL

When raw SQL is unavoidable, always pass user input as bound parameters. Never interpolate $_GET / $_POST values directly into a SQL string.

Store credentials securely

Load database credentials from environment variables (getenv('DB_PASS')) and never commit them to version control.

Use transactions

Wrap any operation that involves more than one related INSERT / UPDATE / DELETE in a transaction so partial failures are automatically rolled back.

Avoid SELECT *

Always name the columns you need. This avoids accidentally exposing sensitive fields (e.g. password) and reduces network overhead.