Skip to content

samuelfaj/PHP-class_db.php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Class_DB

MIT license built with PHP MySQL Ready MySQLI Ready mssql Ready sqlserv Ready pgsql Ready

Write less. Do a lot more. Class_DB allows you to write secure SQL queris easily and work with many different databases using the same syntax

Everytime i was working with or starting a new system, i had to look at the database documentation and choose between create a class for it using mine functions or write the system following his own. But what if the driver changed? Yeah, Everyone who is here for a while saw some changes in PHP drivers. Those changes were a painful work for great systems and even more for small teams. We saw mssql become sqlserv, mysql become mysqli and it's common to see groups deciding to change the database in the middle of the project.

Thinking about it i developed class_db. It helps us to write less SQL and don't care for the chosen database. Just write your code and class_db will do the rest. If the driver or the team resolves to change, just update it!

Install

It's simple, just download and require it.

composer require samuelfaj/class_db

Examples

Initializing

<?php
    require_once 'vendor/autoload.php';

    $db = new ClassDb\Db(array(
        'host'     => 'localhost'      ,  // string - Host of Connection.
        'user'     => 'username'       ,  // string - Database's User.
        'password' => 'mysecretpass'   ,  // string - User's Password.
        'database' => 'myapplication'  ,  // string - Default Database name.
        'db_type'  => 'mysql'          ,  // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
    ));

    $sql = new ClassDb\Query($db);

Doing a simple query passing it as text:

<?php
    require_once 'vendor/autoload.php';

    $db = new ClassDb\Db(array(
        'host'     => 'localhost'      ,  // string - Host of Connection.
        'user'     => 'username'       ,  // string - Database's User.
        'password' => 'mysecretpass'   ,  // string - User's Password.
        'database' => 'myapplication'  ,  // string - Default Database name.
        'db_type'  => 'mysql'          ,  // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
    ));

    $sql = new ClassDb\Query($db);
    $sql->exec("SELECT * FROM users");
    var_dump($sql->query);

Doing a select without write one single character of SQL:

<?php
    require_once 'vendor/autoload.php';

    $sql = new ClassDb\Query(array(
        'host'     => 'localhost'      ,  // string - Host of Connection.
        'user'     => 'username'       ,  // string - Database's User.
        'password' => 'mysecretpass'   ,  // string - User's Password.
        'database' => 'myapplication'  ,  // string - Default Database name.
        'db_type'  => 'mysql'          ,  // string - Type of Database. (It can be: 'mysql', 'mysqli' , 'mssql' , 'sqlserv' , 'pgsql').
    ));

    $sql->table('users');
    $sql->limit(array(0,10));
	
    var_dump($sql->select());

Selecting and updating a user without write one single character of SQL:

<?php
    require_once 'vendor/autoload.php';

    $sql = new ClassDb\Query($db);
    $sql->table('users');
    $sql->where(
        array('id'    , $_POST['id']),
        array('email' , $_POST['email'])
    );
    $sql->order('id','DESC');
    $sql->limit(1);

    if( $sql->select()->have_rows ){ 
        $sql->update(array('active' => 0)); 
    }

License

This project is licensed under the MIT License - see the LICENSE file for details