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 |
|---|---|---|
|
Yes |
Database driver. See Supported Drivers below. Must be lowercase. |
|
Yes |
Hostname or IP address of the database server (e.g. |
|
No |
Port number. Defaults to the driver’s standard port when omitted. |
|
Yes |
Database user with appropriate privileges. |
|
Yes |
Password for the database user. Use an empty string for no password. |
|
Yes |
Name of the database to connect to. |
|
No |
Connection character set. Recommended: |
|
No |
Optional prefix prepended to every table name by the Query Builder. Useful when multiple applications share the same database. |
|
yes |
Required for SQLite connections. Specifies the full file path to the SQLite database file
(for example: |
Supported Drivers
Driver value |
Database |
Default port |
Notes |
|---|---|---|---|
|
MySQL / MariaDB |
|
Recommended for most applications. Requires the PHP |
|
PostgreSQL |
|
Requires the PHP |
|
Microsoft SQL Server |
|
Requires the PHP |
|
SQLite 3 |
n/a |
File-based. Set |
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 |
Store credentials securely |
Load database credentials from environment variables
( |
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 |
Always name the columns you need. This avoids accidentally exposing
sensitive fields (e.g. |