hadi/database

Simple database driver for mysql (PDO)

1.0.1 2017-09-10 12:25 UTC

This package is auto-updated.

Last update: 2024-04-22 22:08:27 UTC


README

Updated version can be found here https://github.com/im4aLL/roolith-database

PDO MySql driver class for PHP

Introduction

This is simple class for SELECT, INSERT, UPDATE, DELETE query for MySQL

Installation

composer require hadi/database

if you don't want composer then simple grab class file from src/Database.php and use it!

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:

$db->table('users')->insert(['name' => 'John doe', 'email' => 'john@email.com']);

Insert data when supplied email john@email.com not exists in table users:

$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

$db->table('users')->update(
    ['name' => 'John doe', 'email' => 'john@email.com'],
    ['id' => 1]
);

or

$db->table('users')->update(
    ['username' => 'johndoe'],
    'id = 1'
);

update username if nobody else using same username

$db->table('users')->update(
    ['username' => 'johndoe'],
    ['id' => 4],
    ['username']
);
result
affected_row
is_duplicate

Delete

$db->table('users')->delete(['id' => 4]);
result
affected_row