softon/mysqli-dbclass

A simple Object Oriented approach for accessing Mysql Databases.

v1.0.0 2015-08-07 10:38 UTC

This package is auto-updated.

Last update: 2024-04-13 22:48:01 UTC


README

MySQLi Database Class for PHP Developers

A simple Object Oriented approach for accessing Mysql Databases. This class will make your life easy while developing your products and saves a lot of time that is wasted while creating simple CRUD operations. This class also suports Transactions. Good Error Reporting and Debuging features. Additional quick commands that provide better security and ease of developing. Refer example.php for all the sample code.

This class is an enhanced version of PHP MySQL wrapper v3 by ricocheting.com. Hence it is compatible with the class. To use the enhanced MySQL Wrapper simply include the Database.class.php in your code and everything should work as before.

How to Setup

Before you start, edit the config.inc.php to match your database credentials. Then include the Class file and config file on the top of the code where you want to use the class as show below.


include_once('config.inc.php');
include_once('Database.php');

Create a DB object to interact with the class.


$db = new Database(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);

You are all set to run any query with this $db object. If you are inside a function or a another class you may call the obtain function to recover the $db object.


$db = new Database::obtain();

How to Use

//Simple Raw Query
$result = $db->query("SELECT * FROM authors")->fetch();

//Parametrised Query and fetch one Record
$result = $db->query("SELECT * FROM :table WHERE :name = ':value'",['table'=>'authors','name'=>'paperid','value'=>'P12206436'])->fetch();

//Parametrised Query and fetch all Records
$result_array = $db->query("SELECT * FROM :table WHERE :name = ':value'",['table'=>'authors','name'=>'paperid','value'=>'P12206436'])->fetch_all();

// Find a row by id and fetch
$result = $db->findById('authors',1);

// Find a row by column and fetch
$result = $db->findByCol('authors','paperid','P12206436');


// Count All rows in a table
$result = $db->count('authors');

// Count All rows in a table with condition
$result = $db->count('authors'," astatus='Approved'");

// Update a row
$result = $db->update('authors',['paperid'=>'SHIBU']," id = '1'");

// Update a row with parameter protection
$result = $db->update('authors',['paperid'=>'SHIBU','aname'=>'test','aorg'=>'test']," id = '1'",['paperid','aname']);

// Bulk Update a row with parameter protection without transaction
$result = $db->bulk_update('authors',array(
    1=> array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    2=> array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),['paperid','aname']);


// Bulk Update a row without parameter protection with transaction
$result = $db->bulk_update('authors',array(
    1=> array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    2=> array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),null,true);

// Insert Data
$result = $db->insert('authors',['paperid'=>rand(1234,99999),'aname'=>'Test Ing']);

// Bulk Insert with parameter protection with transaction
$result = $db->bulk_insert('authors',array(
    array('paperid'=>'SHIBU','aname'=>'test','aorg'=>'test'),
    array('paperid'=>'SHIBU2','aname'=>'test2','aorg'=>'test2')
),['paperid','aname'],true);


// Delete a record
$result = $db->delete('authors'," id=1 ");