ModernPDO
ModernPDO is a simple library for PHP v8.1
Getting Started
Download the latest release, create the directory ModernPDO
in your library directory, and drop ModernPDO/src/
into [lib-dir]/ModernPDO/
.
Create the new ModernPDO instance:
$modernPDO = new \ModernPDO\ModernPDO(
$type, // (mysql, etc.)
$charset, // (utf8, utf8mb4, etc.)
$host, // (localhost, etc.)
$username, // user's name
$password, // user's password
$database, // database's name
);
Examples
Queries Examples
Source
// get all accounts
$modernPDO->exec("SELECT * FROM `account` WHERE 1");
Prepared
// get all accounts where balance >= ?
$accounts = $modernPDO->query("SELECT * FROM `account` WHERE `balance` >= ?", [1000])->fetchAll();
// get one account where name == ?
$account = $modernPDO->query("SELECT * FROM `account` WHERE `name` = ?", ["StulE"])->fetch();
CRUD Examples
DELETE
$modernPDO->delete($table)->where($col, $val)->execute();
INSERT
// long syntax
$modernPDO->insert($table)->values([$col1 => $val1, ...])->execute();
// short syntax
$modernPDO->insert($table, [$col1 => $val1, ...])->execute();
SELECT
// get all rows from $table
$modernPDO->select($table)->all();
// get rows from $table where $col == $val
$modernPDO->select($table)->where($col, $val)->one();
// get first row from $table by $order where $col == $val
$modernPDO->select($table)->where($col, $val)->firstBy($order);
// get last row from $table by $order where $col == $val
$modernPDO->select($table)->where($col, $val)->lastBy($order);
// long syntax
$modernPDO->select($table)->columns([$col1, $col2, ...])->all();
// short syntax
$modernPDO->select($table, [$col1, $col2, ...])->all();
UPDATE
// long syntax
$modernPDO->update($table)->set([$col1 => $val1, ...])->where($col, $val)->execute();
// short syntax
$modernPDO->update($table, [$col1 => $val1, ...])->where($col, $val)->execute();