Skip to content

ch10_databases

Daniel Samson edited this page Feb 19, 2025 · 2 revisions

Teensy PHP supports MySQL, PostgreSQL, SQLite, and MSSQL.

Initializing the Database

To initialize the database, you can use the Database::connect function.

use TeensyPHP\Utility\Database;

$db = Database::connect(
    Config::get("DATABASE_ENGINE", "sqlite"),
    Config::get("DATABASE_DATABASE", "teensy.sqlite"),
    Config::get("DATABASE_HOST"),
    Config::get("DATABASE_PORT"),
    Config::get("DATABASE_USERNAME"),
    Config::get("DATABASE_PASSWORD"),
);

Using the Database

Once the database is connected, you can use the Database->connection() function to get the PDO connection.

$sql = "SELECT * FROM {$table} WHERE id = ?";
$statement = Database::connection()->prepare($sql);
$result = $statement->execute([$id]);
$records = $statement->fetchAll(\PDO::FETCH_ASSOC);

Clone this wiki locally