A PHP library to access a database.
composer require michaelrushton/databaseuse MichaelRushton\Database\Connections\MariaDBConnection;
use MichaelRushton\Database\Drivers\MariaDBDriver;
$connection = new MariaDBConnection(new MariaDBDriver());$driver = new MariaDBDriver(
$username,
$password,
$host,
$port,
$dbname,
$unix_socket,
$charset,
$pdo_options
);use MichaelRushton\Database\Connections\MySQLConnection;
use MichaelRushton\Database\Drivers\MySQLDriver;
$connection = new MySQLConnection(new MySQLDriver());$driver = new MySQLDriver(
$username,
$password,
$host,
$port,
$dbname,
$unix_socket,
$charset,
$pdo_options
);use MichaelRushton\Database\Connections\PostgreSQLConnection;
use MichaelRushton\Database\Drivers\PostgreSQLDriver;
$connection = new PostgreSQLConnection(new PostgreSQLDriver());$driver = new PostgreSQLDriver(
$username,
$password,
$host,
$port,
$dbname,
$sslmode,
$pdo_options
);use MichaelRushton\Database\Connections\SQLiteConnection;
use MichaelRushton\Database\Drivers\SQLiteDriver;
$connection = new SQLiteConnection(new SQLiteDriver());$driver = new SQLiteDriver(
$database,
$pdo_options
);use MichaelRushton\Database\Connections\SQLServerConnection;
use MichaelRushton\Database\Drivers\SQLServerDriver;
$connection = new SQLServerConnection(new SQLServerDriver());$driver = new SQLServerDriver(
$username,
$password,
$Server,
$AccessToken,
$APP,
$ApplicationIntent,
$AttachDBFileName,
$Authentication,
$ColumnEncryption,
$ConnectionPooling,
$ConnectRetryCount,
$ConnectRetryInterval,
$Database,
$Driver,
$Encrypt,
$Failover_Partner,
$KeyStoreAuthentication,
$KeyStorePrincipalId,
$KeyStoreSecret,
$Language,
$LoginTimeout,
$MultipleActiveResultSets,
$MultiSubnetFailover,
$QuotedId,
$Scrollable,
$TraceFile,
$TraceOn,
$TransactionIsolation,
$TransparentNetworkIPResolution,
$TrustServerCertificate,
$WSID,
$pdo_options
);use MichaelRushton\Database\Configuration;
$connection = new SQLiteConnection(new SQLiteDriver(), new Configuration(
cache_prepared_statements: true
));$driver = $connection->driver;$configuration = $connection->configuration;$cache = $connection->cache;$event_manager = $connection->event_manager;See https://www.php.net/manual/en/pdo.connect.php. Will not reconnect if a connection is open.
$connection->connect();$pdo = $connection->pdo;See https://www.php.net/manual/en/pdo.exec.php.
$count = $connection->exec("DELETE FROM users");See https://www.php.net/manual/en/pdo.query.php.
$pdo_stmt = $connection->query("SELECT * FROM users");
$pdo_stmt = $connection->query("SELECT * FROM users", $fetchMode, ...$fetchModeArgs);See https://www.php.net/manual/en/pdo.prepare.php.
Note: Prepared statements are cached and reused unless Configuration#cache_prepared_statements is set to false.
$pdo_stmt = $connection->prepare("SELECT * FROM users WHERE id = ?");
$pdo_stmt = $connection->prepare("SELECT * FROM users WHERE id = ?", $options);See https://www.php.net/manual/en/pdo.prepare.php.
$pdo_stmt = $connection->uncached("SELECT * FROM users WHERE id = ?");
$pdo_stmt = $connection->uncached("SELECT * FROM users WHERE id = ?", $options);See https://www.php.net/manual/en/pdostatement.bindvalue.php.
$pdo_stmt = $connection->bindValues("SELECT * FROM users WHERE id = ?", [1]);See https://www.php.net/manual/en/pdostatement.execute.php.
$pdo_stmt = $connection->execute("SELECT * FROM users WHERE id = ?", [1]);See https://www.php.net/manual/en/pdostatement.fetch.php.
$user = $connection->fetch("SELECT * FROM users WHERE id = ?", [1]);
$user = $connection->fetch("SELECT * FROM users WHERE id = ?", [1], $mode, $cursorOrientation, $cursorOffset);See https://www.php.net/manual/en/pdostatement.fetchall.php.
$users = $connection->fetchAll("SELECT * FROM users WHERE active = ?", [1]);
$users = $connection->fetchAll("SELECT * FROM users WHERE active = ?", [1], $mode, ...$args);See https://www.php.net/manual/en/pdostatement.fetchcolumn.php.
$id = $connection->fetchColumn("SELECT * FROM users WHERE id = ?", [1]);
$id = $connection->fetchColumn("SELECT * FROM users WHERE id = ?", [1], $column);See https://www.php.net/manual/en/pdostatement.fetchobject.php.
$user = $connection->fetchObject("SELECT * FROM users WHERE id = ?", [1]);
$user = $connection->fetchObject("SELECT * FROM users WHERE id = ?", [1], $class, $constructorArgs);See https://www.php.net/manual/en/pdostatement.fetch.php.
$generator = $connection->yield("SELECT * FROM users WHERE active = ?", [1]);
$generator = $connection->yield("SELECT * FROM users WHERE active = ?", [1], $mode, $cursorOrientation, $cursorOffset);See https://www.php.net/manual/en/pdo.begintransaction.php.
$connection->beginTransaction();See https://www.php.net/manual/en/pdo.commit.php.
$connection->commit();See https://www.php.net/manual/en/pdo.rollback.php.
$connection->rollBack();
$connection->rollBack($exception);Begins a transaction, rolls back if an exception is thrown (rethrowing the exception), else commits.
use MichaelRushton\Database\Connections\SQLiteConnection;
$connection->transaction(function (SQLiteConnection $connection)
{
$connection->query("INSERT INTO users (id) VALUES (1)");
$connection->query("INSERT INTO users (id) VALUES (2)");
});$connection->close();use MichaelRushton\Database\Connections\SQLiteConnection;
$connection->when(
value: $value,
if_true: function (SQLiteConnection $connection, $value) {
// ...
},
if_false: function (SQLiteConnection $connection, $value) {
// ...
}
);use MichaelRushton\Database\Connections\SQLiteConnection;
$return = $connection->pipe(function (SQLiteConnection $connection) {
// ...
return true;
});use MichaelRushton\Database\Connections\SQLiteConnection;
$connection->through(function (SQLiteConnection $connection) {
// ...
});See DELETE.
$stmt = $connection->delete();See INSERT.
$stmt = $connection->insert();See REPLACE. MariaDB, MySQL, and SQLite only.
$stmt = $connection->replace();See SELECT.
$stmt = $connection->select();See UPDATE.
$stmt = $connection->update();$connection = $stmt->connection;See https://www.php.net/manual/en/pdo.exec.php.
$count = $stmt->delete()->from('users')->exec();See https://www.php.net/manual/en/pdo.query.php.
$pdo_stmt = $stmt->select()->from('users')->query();
$pdo_stmt = $stmt->select()->from('users')->query($fetchMode, ...$fetchModeArgs);See https://www.php.net/manual/en/pdo.prepare.php.
Note: Prepared statements are cached and reused unless Configuration#cache_prepared_statements is set to false.
$pdo_stmt = $stmt->select()->from('users')->where('id = ?')->prepare();
$pdo_stmt = $stmt->select()->from('users')->where('id = ?')->prepare($options);See https://www.php.net/manual/en/pdo.prepare.php.
$pdo_stmt = $stmt->select()->from('users')->where('id = ?')->uncached();
$pdo_stmt = $stmt->select()->from('users')->where('id = ?')->uncached($options);See https://www.php.net/manual/en/pdostatement.bindvalue.php.
$pdo_stmt = $stmt->select()->from('users')->where('id', 1)->bindValues();See https://www.php.net/manual/en/pdostatement.execute.php.
$pdo_stmt = $stmt->select()->from('users')->where('id', 1)->execute();See https://www.php.net/manual/en/pdostatement.fetch.php.
$user = $stmt->select()->from('users')->where('id', 1)->fetch();
$user = $stmt->select()->from('users')->where('id', 1)->fetch($mode, $cursorOrientation, $cursorOffset);See https://www.php.net/manual/en/pdostatement.fetchall.php.
$users = $stmt->select()->from('users')->where('active', 1)->fetchAll();
$users = $stmt->select()->from('users')->where('active', 1)->fetchAll($mode, ...$args);See https://www.php.net/manual/en/pdostatement.fetchcolumn.php.
$id = $stmt->select()->from('users')->where('id', 1)->fetchColumn();
$id = $stmt->select()->from('users')->where('id', 1)->fetchColumn($column);See https://www.php.net/manual/en/pdostatement.fetchobject.php.
$user = $stmt->select()->from('users')->where('id', 1)->fetchObject();
$user = $stmt->select()->from('users')->where('id', 1)->fetchObject($class, $constructorArgs);See https://www.php.net/manual/en/pdostatement.fetch.php.
$generator = $stmt->select()->from('users')->where('active', 1)->yield();
$generator = $stmt->select()->from('users')->where('active', 1)->yield($mode, $cursorOrientation, $cursorOffset);use MichaelRushton\Database\SQL\Statements\SQLite\SQLiteSelect;
$stmt->when(
value: $value,
if_true: function (SQLiteSelect $stmt, $value) {
// ...
},
if_false: function (SQLiteSelect $stmt, $value) {
// ...
}
);use MichaelRushton\Database\SQL\Statements\SQLite\SQLiteSelect;
$return = $stmt->pipe(function (SQLiteSelect $stmt) {
// ...
return true;
});use MichaelRushton\Database\SQL\Statements\SQLite\SQLiteSelect;
$stmt->through(function (SQLiteSelect $stmt) {
// ...
});$cache->clear();class Event {}
$event_manager->listen(Event::class, function (Event $event)
{
});$event_manager->dispatch(new Event());Connection only.
use MichaelRushton\Database\Events\BeforeConnectEvent;
$connection->beforeConnect(function (BeforeConnectEvent $event)
{
$event->connection;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\AfterConnectEvent;
$connection->afterConnect(function (AfterConnectEvent $event)
{
$event->before_event;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\BeforeBeginTransactionEvent;
$connection->beforeBeginTransaction(function (BeforeBeginTransactionEvent $event)
{
$event->connection;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\AfterBeginTransactionEvent;
$connection->afterBeginTransaction(function (AfterBeginTransactionEvent $event)
{
$event->before_event;
$event->success;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\BeforeCommitEvent;
$connection->beforeCommit(function (BeforeCommitEvent $event)
{
$event->connection;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\AfterCommitEvent;
$connection->afterCommit(function (AfterCommitEvent $event)
{
$event->before_event;
$event->success;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\BeforeRollBackEvent;
$connection->beforeRollBack(function (BeforeRollBackEvent $event)
{
$event->connection;
$event->exception;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\AfterRollBackEvent;
$connection->afterRollBack(function (AfterRollBackEvent $event)
{
$event->before_event;
$event->success;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\BeforeCloseEvent;
$connection->beforeClose(function (BeforeCloseEvent $event)
{
$event->connection;
$event->time;
});Connection only.
use MichaelRushton\Database\Events\AfterCloseEvent;
$connection->afterClose(function (AfterCloseEvent $event)
{
$event->before_event;
$event->time;
});use MichaelRushton\Database\Events\BeforeExecEvent;
$stmt->beforeExec(function (BeforeExecEvent $event)
{
$event->connection;
$event->statement;
$event->time;
});use MichaelRushton\Database\Events\AfterExecEvent;
$stmt->afterExec(function (AfterExecEvent $event)
{
$event->before_event;
$event->count;
$event->time;
});use MichaelRushton\Database\Events\BeforeQueryEvent;
$stmt->beforeQuery(function (BeforeQueryEvent $event)
{
$event->connection;
$event->query;
$event->fetchMode;
$event->fetchModeArgs;
$event->time;
});use MichaelRushton\Database\Events\AfterQueryEvent;
$stmt->afterQuery(function (AfterQueryEvent $event)
{
$event->before_event;
$event->statement;
$event->time;
});use MichaelRushton\Database\Events\BeforePrepareEvent;
$stmt->beforePrepare(function (BeforePrepareEvent $event)
{
$event->connection;
$event->query;
$event->options;
$event->time;
});use MichaelRushton\Database\Events\AfterPrepareEvent;
$stmt->afterPrepare(function (AfterPrepareEvent $event)
{
$event->before_event;
$event->statement;
$event->time;
});use MichaelRushton\Database\Events\BeforeBindValueEvent;
$stmt->beforeBindValue(function (BeforeBindValueEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->param;
$event->value;
$event->time;
});use MichaelRushton\Database\Events\AfterBindValueEvent;
$stmt->afterBindValue(function (AfterBindValueEvent $event)
{
$event->before_event;
$event->success;
$event->time;
});use MichaelRushton\Database\Events\BeforeExecuteEvent;
$stmt->beforeExecute(function (BeforeExecuteEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->params;
$event->time;
});use MichaelRushton\Database\Events\AfterExecuteEvent;
$stmt->afterExecute(function (AfterExecuteEvent $event)
{
$event->before_event;
$event->success;
$event->time;
});use MichaelRushton\Database\Events\BeforeFetchEvent;
$stmt->beforeFetch(function (BeforeFetchEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->params;
$event->model;
$event->cursorOrientation;
$event->cursorOffset;
$event->time;
});use MichaelRushton\Database\Events\AfterFetchEvent;
$stmt->afterFetch(function (AfterFetchEvent $event)
{
$event->before_event;
$event->row;
$event->time;
});use MichaelRushton\Database\Events\BeforeFetchAllEvent;
$stmt->beforeFetchAll(function (BeforeFetchAllEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->params;
$event->mode;
$event->args;
$event->time;
});use MichaelRushton\Database\Events\AfterFetchAllEvent;
$stmt->afterFetchAll(function (AfterFetchAllEvent $event)
{
$event->before_event;
$event->rows;
$event->time;
});use MichaelRushton\Database\Events\BeforeFetchColumnEvent;
$stmt->beforeFetchColumn(function (BeforeFetchColumnEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->params;
$event->column;
$event->time;
});use MichaelRushton\Database\Events\AfterFetchColumnEvent;
$stmt->afterFetchColumn(function (AfterFetchColumnEvent $event)
{
$event->before_event;
$event->value;
$event->time;
});use MichaelRushton\Database\Events\BeforeFetchObjectEvent;
$stmt->beforeFetchObject(function (BeforeFetchObjectEvent $event)
{
$event->connection;
$event->statement;
$event->query;
$event->params;
$event->class;
$event->constructorArgs;
$event->time;
});use MichaelRushton\Database\Events\AfterFetchObjectEvent;
$stmt->afterFetchObject(function (AfterFetchObjectEvent $event)
{
$event->before_event;
$event->object;
$event->time;
});