samuelfaj/class_db

Class DB allows you to write secure SQL queries easily and work with many different databases using the same syntax.

2.1.0 2018-04-06 03:23 UTC

This package is not auto-updated.

Last update: 2024-05-04 16:56:38 UTC


README

68747470733a2f2f7777772e6973736172742e636f6d2f626c6f672f77702d636f6e74656e742f75706c6f6164732f323031372f30332f626f78626172696d616765352e6a7067

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