2017 © Pedro Peláez
 

library dbcon

A PHP API to connect to different RDBMS and NoSQL

image

jkga/dbcon

A PHP API to connect to different RDBMS and NoSQL

  • Wednesday, March 2, 2016
  • by jkga
  • Repository
  • 1 Watchers
  • 1 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

dbcon

A PHP API to connect to different RDBMS and NoSQL ex. mysql, mysqli, postgresSQL, mongoDB etc.., (*1)

Why would you need this

Under the hood, Dbcon uses PDO to connect to different supported database management system and native functions for those who have not. You would need these if you need to define multiple database connection on a single file and establish a connection later. It allows the application to connect to different database management system using a wrapper that can be used by other developer easier and cleaner, (*2)

Benefits

  • cleaner code during development
  • configure multiple different database connection
  • easier to maintain
  • modular (can define your own extension.Please see Extending section)

Note

For small projects and simple application, use native functions to connect and manage your databases.It is advisable only to use dbcon for large projects in such maintainability and productivity are tedious tasks., (*3)

Requirements

  • PHP 5.4 (PHP 5.5+ recommended)
  • PDO driver
  • DBMS driver defined by developer's extended class br/

Currently supported DBMS

  • MySQL (PDO) / MySQLi (native)
  • PostgreSQL (PDO) br/

How to use

Download the zip file to your computer and extract to the root directory of your server. Below is an excerpt script from example/mysql/mysql.example.php file to connect to MySQL. Example database connection scripts are located in /example folder for your reference., (*4)

'localhost',
                'username'=>'root',
                'password'=>'',
                'port'=>'',
                'dbms'=>'mysql',
                'dbname'=>'your_database'
            ];

    #class mysql
    use \DBCon\MySQL\mysql as MySQL;

    #setup database connection
    $db=new MySQL($database['dbms'],$database['host'],$database['username'],$database['password'],$database['dbname']);

    #open connection to mysql
    $sth=$db->open();

    #add query,bind param ,etc. to command. These will depend on your dbms
    $command=$sth->query(...);

    #execute command
    $command->execute();
        
 ?>

br/ You may also configure your settings to shift or use different DBMS without affecting the rest of your codes. Doing so will allow applications to sync their data by reading and writing it on different databases. br/br/ Database Configuration, (*5)

'localhost',
                'username'=>'root',
                'password'=>'',
                'port'=>'',
                'dbms'=>'mysql',
                'dbname'=>'your_database'
            ];

    $database_pgsql=['host'=>'localhost',
            'username'=>'postgres',
            'password'=>'root',
            'port'=>'5432',
            'dbms'=>'pgsql',
            'dbname'=>'test'
        ];


    #class mysql ang pgsql
    use \DBCon\MySQL\mysql as MySQL;
    use \DBCon\pgSQL\pgsql as pgSQL;

    /*setup database connection
     this will remain closed unless explicitly called by '->open()' function */
     
    $main_database=new MySQL($database_mysql['dbms'],$database_mysql['host'],$database_mysql['username'],$database_mysql['password'],$database_mysql['dbname']);
    $for_statistics_purpose=new pgSQL($database_pgsql['dbms'],$database_pgsql['host'],$database_pgsql['username'],$database_pgsql['password'],$database_pgsql['dbname']);

        
 ?>

br/ Page containing CRUD function, (*6)

open();
    #or open connection to pgsql
    //backup database
    $sth=$for_statistics_purpose->open();

    
        
 ?>

br/, (*7)

Extending

Developer can create their own module by extending defined class to dbms under the namespace DBCon\dbms and to make cleaner extension add your defined class on dbcon/your_namespace/your_extension_class, (*8)

Example. dbcon/pgsql/pgsql.php, (*9)

Note: You may use the dbcon_development branch as the core and create a branch with "dbcon_dev_" prefix.].


Removing unwanted module

Developer A and Developer B conversation, (*10)

Do you need mysql?, (*11)

                    I have mysql module

Do you need pgsql?, (*12)

                    Nope! i dont use it and i need a smaller footprint

Fine i will remove pgsql folder :), (*13)

                    wait! i need mongoDB

SOLUTION: Then Create Module <3, (*14)

br/, (*15)

Test

You can find the PHPUnit tests in dbconUnitTesting folder, (*16)

License

This software is under the MIT liscense.Please read LICENSE for information on the software availability and description., (*17)

The Versions

02/03 2016

dev-master

9999999-dev

A PHP API to connect to different RDBMS and NoSQL

  Sources   Download

MIT