A powerful database wrapper library built on top of Medoo, providing a simple and intuitive interface for database operations in PHP applications.
composer require websitesql/database
// Initialize the database provider
$db = new WebsiteSQL\Database\Database([
'type' => 'mysql',
'host' => 'localhost',
'database' => 'name',
'username' => 'your_username',
'password' => 'your_password'
], '../migrations');
// Select all records from users table
$users = $db->select("users", "*");
// Select with conditions
$user = $db->get("users", "*", ["id" => 1]);
// Select with JOIN
$data = $db->select("posts", [
"[>]users" => ["user_id" => "id"]
], [
"posts.id",
"posts.title",
"users.username"
], [
"posts.status" => "published",
"ORDER" => ["posts.created" => "DESC"]
]);
$db->insert("users", [
"username" => "john_doe",
"email" => "john@example.com",
"created" => date("Y-m-d H:i:s")
]);
// Get last inserted ID
$lastId = $db->id();
$db->update("users", [
"email" => "new_email@example.com"
], [
"id" => 1
]);
$db->delete("users", [
"id" => 1
]);
The database library includes a migration system for managing database schema changes:
// Run migrations
$db->migrations()->up();
// Rollback the last batch of migrations
$db->migrations()->down();
// Rollback all migrations
$db->migrations()->reset();
// Refresh migrations (rollback all and run again)
$db->migrations()->refresh();
query(string $statement, array $map = [])
: Execute raw SQL queriesexec(string $statement)
: Execute raw statementcreate(string $table, array $columns, array $options = null)
: Create a tabledrop(string $table)
: Drop a tableselect(string $table, array $join, array|string $columns = null, array $where = null)
: Select dataget(string $table, array $join, array|string $columns = null, array $where = null)
: Get a single recordinsert(string $table, array $values, string $primaryKey = null)
: Insert dataupdate(string $table, array $data, array $where = null)
: Update datadelete(string $table, array $where = null)
: Delete datareplace(string $table, array $columns, array $where = null)
: Replace data
count(string $table, array $join = null, string $column = null, array $where = null)
: Count rowsavg(string $table, array $join, string $column = null, array $where = null)
: Calculate averagemax(string $table, array $join, string $column = null, array $where = null)
: Get maximum valuemin(string $table, array $join, string $column = null, array $where = null)
: Get minimum valuesum(string $table, array $join, string $column = null, array $where = null)
: Calculate sum
action(callable $actions)
: Execute callback in transactionbeginTransaction()
: Begin a transactioncommit()
: Commit a transactionrollBack()
: Rollback a transaction
id(?string $name = null)
: Get last inserted IDpdo()
: Get PDO instancedebug()
: Get the last query for debugginglog()
: Get query loginfo()
: Get database connection infoerror()
: Get error informationrand(string $table, array $join = null, array|string $columns = null, array $where = null)
: Get random recordshas(string $table, array $join, array $where = null)
: Check if records exist
migrations()->up()
: Run pending migrationsmigrations()->down()
: Rollback the last batch of migrationsmigrations()->reset()
: Rollback all migrationsmigrations()->refresh()
: Rollback all and run again
MIT