PHPackages                             laswitchtech/php-database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. laswitchtech/php-database

ActiveLibrary[Database &amp; ORM](/categories/database)

laswitchtech/php-database
=========================

Database Class for PHP

v2.3.10(2y ago)02051GPL-3.0-onlyPHPPHP \*

Since Nov 1Pushed 1y agoCompare

[ Source](https://github.com/LouisOuellet/php-database)[ Packagist](https://packagist.org/packages/laswitchtech/php-database)[ RSS](/packages/laswitchtech-php-database/feed)WikiDiscussions stable Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (44)Used By (1)

[![GitHub repo logo](/dist/img/logo.png)](/dist/img/logo.png)

phpDB - \[DEPRECATED\] - Use [coreDatabase](https://github.com/LaswitchTech/coreDatabase) instead
=================================================================================================

[](#phpdb---deprecated---use-coredatabase-instead)

[![License](https://camo.githubusercontent.com/7bb120323443429b06f80a233acf336ba216cd53aa549fc0f18b3f5b1c10fcd0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/7bb120323443429b06f80a233acf336ba216cd53aa549fc0f18b3f5b1c10fcd0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765)[![GitHub repo size](https://camo.githubusercontent.com/7688ea6e1e9478f89474f967b75237daf82039a482f225f88f0e5a3620f19b40/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562)](https://camo.githubusercontent.com/7688ea6e1e9478f89474f967b75237daf82039a482f225f88f0e5a3620f19b40/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7265706f2d73697a652f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765266c6f676f3d676974687562)[![GitHub top language](https://camo.githubusercontent.com/b19999123e195962e49c2fedbf1a18e870479bdc41d4f40968f9576159012fc2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/b19999123e195962e49c2fedbf1a18e870479bdc41d4f40968f9576159012fc2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f4c6f7569734f75656c6c65742f7068702d64617461626173653f7374796c653d666f722d7468652d6261646765)[![Version](https://camo.githubusercontent.com/09206e535e6e4b80830ed32427d77ab1754b56c03b31e58532c3954afb8423cf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4c6f7569734f75656c6c65742f7068702d64617461626173653f6c6162656c3d56657273696f6e267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/09206e535e6e4b80830ed32427d77ab1754b56c03b31e58532c3954afb8423cf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f4c6f7569734f75656c6c65742f7068702d64617461626173653f6c6162656c3d56657273696f6e267374796c653d666f722d7468652d6261646765)

Description
-----------

[](#description)

This is a PHP class that provides an interface for interacting with a MySQL database using the mysqli extension. It provides methods for creating, reading, updating, and deleting data from a database, as well as for creating and modifying database tables. It also includes methods for transaction handling and error logging.

The class uses prepared statements to prevent SQL injection attacks and supports UTF-8 encoding. It also includes debugging functionality that allows you to log queries and parameters to a file.

Features
--------

[](#features)

- Connection pooling for improved performance
- Automatic database schema migration
- Query builder for easier construction of complex SQL queries
- Support for transactions and rollbacks
- Query profiling and optimization
- Easy and secure interaction with a SQL database
- Debugging functionality
- Simplified handling of common SQL tasks
- Backup and restore your database
- Create database schema for future upgrades
- Upgrade database to a new schema

Why you might need it?
----------------------

[](#why-you-might-need-it)

phpDB is a simple and easy-to-use PHP class that provides an interface for interacting with a MySQL database using the mysqli extension. If you're building a web application or website that needs to store and retrieve data from a MySQL database, then phpDB can save you a lot of time and effort. The class provides methods for creating, reading, updating, and deleting data from a database, as well as for creating and modifying database tables. It uses prepared statements to prevent SQL injection attacks and supports UTF-8 encoding, which ensures that your data is stored and retrieved accurately. Additionally, phpDB includes debugging functionality that allows you to log queries and parameters to a file, making it easier to troubleshoot issues with your database. Whether you're a beginner or an experienced developer, phpDB can simplify your database interactions and help you get your web application up and running quickly.

Can I use this?
---------------

[](#can-i-use-this)

Sure!

License
-------

[](#license)

This software is distributed under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html) license. Please read [LICENSE](LICENSE) for information on the software availability and distribution.

Requirements
------------

[](#requirements)

PHP &gt;= 5.5.0

To Do
-----

[](#to-do)

- Multiple Database Types Support (ex: MariaDB, MySQL, JSON, XML, PostgreSQL, SQLite)

Security
--------

[](#security)

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation
------------

[](#installation)

Using Composer:

```
composer require laswitchtech/php-database
```

How do I use it?
----------------

[](#how-do-i-use-it)

In this documentations, we will use a table called users for our examples.

### Examples

[](#examples)

#### Connecting Database

[](#connecting-database)

##### Using Constant

[](#using-constant)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Define Connection Information
define("DB_HOST", "localhost");
define("DB_USERNAME", "demo");
define("DB_PASSWORD", "demo");
define("DB_DATABASE_NAME", "demo");

//Optionally Output Debug Information
define("DB_DEBUG", true);

//Connect SQL Database
$phpDB = new Database();
```

##### Without Using Constant

[](#without-using-constant)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");
```

#### Create a Table

[](#create-a-table)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Create Table
$boolean = $phpDB->create('users',[
  'id' => [
    'type' => 'BIGINT(10)',
    'extra' => ['UNSIGNED','AUTO_INCREMENT','PRIMARY KEY']
  ],
  'username' => [
    'type' => 'VARCHAR(60)',
    'extra' => ['NOT NULL','UNIQUE']
  ],
  'password' => [
    'type' => 'VARCHAR(100)',
    'extra' => ['NOT NULL']
  ],
  'token' => [
    'type' => 'VARCHAR(100)',
    'extra' => ['NOT NULL','UNIQUE']
  ],
  'created' => [
    'type' => 'DATETIME',
    'extra' => ['DEFAULT CURRENT_TIMESTAMP']
  ]
]);

//Output Result
echo json_encode($boolean, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Alter a Table

[](#alter-a-table)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Alter Table
$boolean = $phpDB->alter('users',[
  'email' => [
    'action' => 'ADD',
    'type' => 'VARCHAR(60)',
    'extra' => ['NOT NULL']
  ],
  'status' => [
    'action' => 'ADD',
    'type' => 'INT(1)',
    'extra' => ['NOT NULL','DEFAULT 0']
  ]
]);

//Output Result
echo json_encode($boolean, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Truncate a Table

[](#truncate-a-table)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Alter Table
$boolean = $phpDB->truncate('users');

//Output Result
echo json_encode($boolean, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Drop a Table

[](#drop-a-table)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Alter Table
$boolean = $phpDB->drop('users');

//Output Result
echo json_encode($boolean, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Insert Data

[](#insert-data)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Insert Query
$id = $phpDB->insert("INSERT INTO users (username, email, status) VALUES (?,?,?)", ["user","user@domain.com",1]);

//Output Result
echo json_encode($id, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Select Data

[](#select-data)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Select Query
$users = $phpDB->select("SELECT * FROM users ORDER BY id ASC LIMIT ?", ["i", 10]);

//Output Result
echo json_encode($users, JSON_PRETTY_PRINT);
```

#### Update Data

[](#update-data)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Update Query
$result = $phpDB->update("UPDATE users SET username = ?, email = ? WHERE id = ?", ["user".$id,"user".$id."@domain.com",$id]);

//Output Result
echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Delete Data

[](#delete-data)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Delete Query
$result = $phpDB->delete("DELETE FROM users WHERE id = ?", [$users[0]['id']]);

//Output Result
echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Backup

[](#backup)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Backup Database
$result = $phpDB->backup();

//Output Result
echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Restore

[](#restore)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Restore Database
$phpDB->restore();
```

#### Schema

[](#schema)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Create a Database Schema
$result = $phpDB->schema();

//Output Result
echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL;
```

#### Upgrade

[](#upgrade)

```
//Import Database class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpDB\Database;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Connect SQL Database
$phpDB = new Database("localhost","demo","demo","demo");

//Upgrade Database to Latest Schema
$phpDB->upgrade();
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Recently: every ~24 days

Total

43

Last Release

1035d ago

Major Versions

v1.1 → v2.02022-11-01

PHP version history (3 changes)v1.0PHP &gt;=5.5.0

v2.1.5PHP ^8.0

v2.1.9PHP \*

### Community

Maintainers

![](https://www.gravatar.com/avatar/1fbdd49d98caf05990260b5ece6a0e9539ca2d448b4af10ebde6ed7d6688c61d?d=identicon)[LouisOuellet](/maintainers/LouisOuellet)

---

Top Contributors

[![LouisOuellet](https://avatars.githubusercontent.com/u/25933907?v=4)](https://github.com/LouisOuellet "LouisOuellet (53 commits)")

### Embed Badge

![Health badge](/badges/laswitchtech-php-database/health.svg)

```
[![Health](https://phpackages.com/badges/laswitchtech-php-database/health.svg)](https://phpackages.com/packages/laswitchtech-php-database)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
