Skip to content

PHP class for SELECT, INSERT, UPDATE, DELETE MySQL data using PDO

Notifications You must be signed in to change notification settings

im4aLL/pdo-mysql-driver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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' => '[email protected]']);

Insert data when supplied email [email protected] not exists in table users:

$db->table('users')->insert(
    ['name' => 'John doe', 'email' => '[email protected]'],
    ['email']
);
result
affected_row
inserted_id
is_duplicate

Update

Update data where id = 1

$db->table('users')->update(
    ['name' => 'John doe', 'email' => '[email protected]'],
    ['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

About

PHP class for SELECT, INSERT, UPDATE, DELETE MySQL data using PDO

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages