GeniXCMS

Db Class

categoryAPI edit_calendar31 Mar 2026

DB Class


The Db class is the foundational database abstraction layer for GeniXCMS. It uses PDO to handle secure, prepared-statement queries across multiple database drivers (MySQL, PostgreSQL, SQLite).

priority_high
ImportantSince version 2.0.0, it is highly recommended to use the Query Builder or the ORM / Model layer for standard data operations. They provide a more maintainable, expressive, and safer interface than direct Db calls.

Connection

Database credentials are automatically loaded from inc/config/config.php. The connection is established during system initialization.

Query Method

Usage: Db::query(string $query, array $params = []);

Executes a SQL query. It is highly recommended to use the $params array for automatic escaping (prepared-statement style).

Db::query("UPDATE `users` SET `status` = '1' WHERE `id` = ?", [5]);

Result Method

Usage: $data = Db::result(string $query, array $params = []);

Executes a SELECT query and returns an array of result objects (stdClass).

$users = Db::result("SELECT * FROM `user` WHERE `group` = ?", [0]);
foreach ($users as $u) {
    echo $u->userid;
}

Insert Method

Usage: Db::insert(array $vars); or Db::insert(string $query);

Inserts a new record into a table.

Array Mode (Recommended)

$data = array(
    'table' => 'posts',
    'key'   => array(
        'title'   => 'My Post',
        'content' => 'Hello World'
    )
);
Db::insert($data);

Update Method

Usage: Db::update(array $vars); or Db::update(string $query);

Updates existing records.

Array Mode (Recommended)

$data = array(
    'table' => 'posts',
    'id'    => 12, // The record ID
    'key'   => array(
        'title' => 'Updated Title'
    )
);
Db::update($data);

Delete Method

Usage: Db::delete(array $vars);

Deletes records from a table.

$data = array(
    'table' => 'posts',
    'where' => array('id' => 1)
);
Db::delete($data);

Helper Properties

  • Db::$num_rows: Stores the number of rows affected/returned by the last query.
  • Db::$last_id: Stores the ID generated by the last INSERT query.

Escape Method

Usage: Db::escape(string $string);

Returns a string safe for SQL insertion. Note: Manual escaping is unnecessary when using the $params argument in query() or result().