PHP code example of hasan-22 / query-builder

1. Go to this page and download the library: Download hasan-22/query-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hasan-22 / query-builder example snippets

 


return [

    'default'=>'mysql',

    'driver'=>[
        'mysql'=>[
            'host'=>'localhost',
            'username'=>'root',
            'password'=>'',
            'dbname'=>'php',
            'charset'=>'UTF8',
        ]
    ]
];
 


class Mysql implements \App\Database\ConnectionInterface
{
    private static $instance;

    private function __construct(){}

    /**
     * This method prevents the creation of duplicate object
     * @return mixed
     */
    public static function setInstance(){
        if(!static::$instance instanceof Mysql){
            static::$instance = new Mysql();
        }
        return static::$instance;
    }

    /**
     * Get database connection information in configuration
     * @return array
     */
    public function driver(): array{
        /**
        * If you have set the database in the config file, use this code.
        */
        // Path of your config file
        $config = ATURAL
        ];
        return \App\Database\PDO::connect($this->driver(),$attributes);
    }
}

$db = new \App\Builder\DB("\App\Database\Mysql");
 
$db->table('users')->get()

$db->table('users')->get('name','password','email')

$db->table('users')->all()

$db->table('users')->find()
// or
$db->table('users')->find(1,2,3)

$db->table('users')->where('name','=','armia')->get();

// for `or where`
$db->table('users')->orWhere('name','=','armia')->get();

// for `where in`
$db->table('users')->whereIn('id',[1,2,3])->get();

// for `or wher in`
$db->table('users')->orWhereIn('id',[1,2,3])->get();

// for `where between`
$db->table('users')->whereBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `or where between`
$db->table('users')->orWhereBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `exists`
$db->table('brands')->exists('users','users.id = brands.user_id')->get();

// And you can run all the functions one after the other like the example below
$db->table('users')
->where('name','=','armia')
->orWhere('name','=','armia')
->whereIn('id',[1,2,3])
->orWhereIn('id',[1,2,3])
->whereBetween('crated_at',['2022-12-12','2023-01-29'])
->orWhereBetween('crated_at',['2022-12-12','2023-01-29'])
->get()

$db->table('users')->havin('name','=','armia')->get();

// for `or where`
$db->table('users')->orHaving('name','=','armia')->get();

// for `where in`
$db->table('users')->havingIn('id',[1,2,3])->get();

// for `or wher in`
$db->table('users')->orHavingIn('id',[1,2,3])->get();

// for `where between`
$db->table('users')->havingBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `or where between`
$db->table('users')->orHavingBetween('crated_at',['2022-12-12','2023-01-29'])->get();


// And you can run all the functions one after the other like the example below
$db->table('users')
->having('name','=','armia')
->orHaving('name','=','armia')
->havingIn('id',[1,2,3])
->orHavingIn('id',[1,2,3])
->havingBetween('crated_at',['2022-12-12','2023-01-29'])
->orHavingeBetween('crated_at',['2022-12-12','2023-01-29'])
->get()

$db->table('users')->innerJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->leftJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->rightJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->crossJoin('brands')->get();


$db->table('users')
->innerJoin('brands','brands.user_id','users.id')
->where('users.id','=',14)->get();

$data = ['name'=>'armia','email'=>'[email protected]','password'=>'123456'];
$db->table('users')->create($data);

$data = [
    ['name'=>'armia','email'=>'[email protected]','password'=>'123456'],
    ['name'=>'armia','email'=>'[email protected]','password'=>'123456'],
    ['name'=>'armia','email'=>'[email protected]','password'=>'123456'],
];
$db->table('users')->create($data);

$db->table('users')->where('id','=',1)->update(['name'=>'update name','email'=>'[email protected]']);
// or
$db->table('users')->update(['name'=>'update name','email'=>'[email protected]'],['id'=>1]);
// or
$db->table('users')->update(['name'=>'update name','email'=>'[email protected]'],['id'=>1,'status'=>'activated']);

$db->table('users')->where('id',1)->delete();
// or
$db->table('users')->delete(['id',1]);
// or
$db->table('users')->delete(['id',1,'status'=>'activated']);


$db->beginTransaction();
try{
    // TODO: code
    
    $db->commit();
}catch (Exception $e){
    $db->rollback();
    // TODO: code
}

$db->table('users')
->innerJoin('brands','brands.user_id','users.id')
->where('users.id','=',14)->debug();

/**
Output:
SQL: [86] SELECT * FROM users INNER JOIN brands ON brands.user_id = users.id WHERE users.id = ?
Sent SQL: [89] SELECT * FROM users INNER JOIN brands ON brands.user_id = users.id WHERE users.id = '14'
Params:  1
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
NULL
*/