A secure, fluent, and lightweight PHP MySQL database query builder and executor class with support for prepared statements, joins, aggregation, pagination, and schema creation.
- Fluent interface for building queries:
select(),where(),join(),groupBy(),orderBy(), etc. - Safe parameter binding using PDO prepared statements to prevent SQL injection.
- Support for single and multiple
whereclauses,whereIn,like,between. - Aggregate functions:
count(),sum(),avg(),min(),max(). - Insert with retrieval of last inserted ID (
insertGetId()). - Table creation and dropping with
createTable()anddropTable(). - Pagination support returning JSON output.
- Configurable via
.envfile. - Debug mode enabled via environment variable.
- Written as a single PHP file with no dependencies.
- Clone or download this repository.
https://github.com/tasherul/easy_db.git-
Create a
.envfile in the same directory with your database credentials:DB_HOST=localhost DB_NAME=your_database DB_USER=your_user DB_PASS=your_password DB_DEBUG=true -
Include or require the PHP file in your project:
require 'DB.php';
-
Instantiate and use the
DBclass:$db = new DB(); $users = $db->table('users')->where('status', 'active')->select('*')->run(); print_r($users);
See the bottom of the PHP file for extensive usage examples including table creation, inserting, selecting, updating, deleting, joins, aggregates, and pagination.
DB_HOST- Database hostDB_NAME- Database nameDB_USER- Database userDB_PASS- Database passwordDB_DEBUG- Enable debug output (trueorfalse)
Below are detailed examples demonstrating every main function of the class:
$db = new DB();$db->createTable('test_users', [
'id' => 'INT PRIMARY KEY AUTO_INCREMENT',
'name' => 'VARCHAR(100) NOT NULL',
'email' => 'VARCHAR(100) UNIQUE',
'status' => 'VARCHAR(50)',
'created_at' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP'
]);$db->table('test_users')->insert([
'name' => 'John Doe',
'email' => 'john@example.com',
'status' => 'active'
]);$newUserId = $db->table('test_users')->insertGetId([
'name' => 'Charlie',
'email' => 'charlie@example.com',
'status' => 'pending'
]);
echo "New user inserted with ID: $newUserId\n";$allUsers = $db->table('test_users')->select('*')->run();
print_r($allUsers);$activeUsers = $db->table('test_users')->where('status', 'active')->run();
print_r($activeUsers);$bobActive = $db->table('test_users')->where(['status' => 'active', 'name' => 'Bob Johnson'])->run();
print_r($bobActive);$usersByIds = $db->table('test_users')->whereIn('id', [1, 2])->run();
print_r($usersByIds);$aliceLike = $db->table('test_users')->like('name', 'Alice')->run();
print_r($aliceLike);$dateRangeUsers = $db->table('test_users')->between('created_at', '2000-01-01', '2100-01-01')->run();
print_r($dateRangeUsers);$userStatusCounts = $db->table('test_users')->select('status, COUNT(*) AS total')->groupBy('status')->run();
print_r($userStatusCounts);$orderedUsers = $db->table('test_users')->select('*')->orderBy('name DESC')->run();
print_r($orderedUsers);$db->table('test_users')->where('id', $newUserId)->update(['status' => 'active']);$activeCount = $db->table('test_users')->where('status', 'active')->count();
echo "Number of active users: $activeCount\n";$db->table('test_users')->where('id', $newUserId)->delete();$user = $db->table('test_users')->where('id', 1)->first();
print_r($user);$paginatedJson = $db->table('test_users')->paginate(2, 1)->paginateJson();
echo $paginatedJson;$db->dropTable('test_users');This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Feel free to open issues or pull requests.
Made with ❤️ by [Tasherul Islam] @tasherul