PHP code example of roi / sql-clause-builder

1. Go to this page and download the library: Download roi/sql-clause-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/ */

    

roi / sql-clause-builder example snippets


$searchable_params = [
    'name'=>[
        'col'=> 'merchants.name', 
        'factors'=>[
            'like',
            'equalto'
        ], 
        'bind_as'=>PDO::PARAM_STR
    ],
    'type'=>[
        'col'=> 'merchants.type', 
        'factors'=>[
            'equalto'
        ], 
        'bind_as'=>PDO::PARAM_STR
    ],
] ;

$clausebuilder = new \Roi\SqlClauseBuilder\SqlClauseBuilder($searchable_params) ;

$data = ['name'=>['data'=>['Bob%'],'factor'=>'like'] ] ;
$clause = $clausebuilder->build($data) ; // returns ['clause'=> 'merchants.name LIKE ?', 'binds'=>[ ['Bob%',PDO::PARAM_STR] ] ]

[
    'clause'=> 'merchants.name LIKE ?', 
    'binds'=>[ 
        [
            'Bob%',PDO::PARAM_STR
        ] 
    ] 
]

$query = $Pdo->prepare("SELECT * FROM table WHERE $clause['clause'] ") ;
foreach($clause['binds'] as $key=>$val){
  $query->bindParam($val[0], $val[1]) ;
}
$query->execute() ;