Updated version can be found here https://github.com/im4aLL/roolith-database, (*1)
PDO MySql driver class for PHP
Introduction
This is simple class for SELECT, INSERT, UPDATE, DELETE query for MySQL, (*2)
Installation
composer require hadi/database
, (*3)
if you don't want composer then simple grab class file from src/Database.php and use it!, (*4)
Usage
Connection
$config = [
'host' => 'localhost',
'name' => 'temp',
'username' => 'root',
'password' => '',
];
$db = new \Hadi\Database();
$db->connect($config);
Disconnect
$db->disconnect();
Select Query
Method #1
$db->query('SELECT * FROM users')->get();
$db->query('SELECT * FROM users')->first();
Method #2
$db->table('users')->select([
'field' => ['name', 'username'],
])->first();
$db->table('users')->select([
'field' => ['name', 'username'],
'condition' => 'WHERE id > 0',
'limit' => '0, 10',
'orderby' => 'name',
'groupby' => 'name',
])->first();
Insert
Insert data:, (*5)
$db->table('users')->insert(['name' => 'John doe', 'email' => 'john@email.com']);
Insert data when supplied email john@email.com
not exists in table users
:, (*6)
$db->table('users')->insert(
['name' => 'John doe', 'email' => 'john@email.com'],
['email']
);
result
affected_row
inserted_id
is_duplicate
Update
Update data where id = 1
, (*7)
$db->table('users')->update(
['name' => 'John doe', 'email' => 'john@email.com'],
['id' => 1]
);
or, (*8)
$db->table('users')->update(
['username' => 'johndoe'],
'id = 1'
);
update username
if nobody else using same username, (*9)
$db->table('users')->update(
['username' => 'johndoe'],
['id' => 4],
['username']
);
result
affected_row
is_duplicate
Delete
$db->table('users')->delete(['id' => 4]);
result
affected_row