PHP code example of efoft / query-builder

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

    

efoft / query-builder example snippets


// select
$q->from('table1,  table2 ')->from('table2')->select(array('table2.age'=>'a','table1.name'=>'n'))->where(array('id'=>13));
$q->where(array('age'=>'/3.*/'))->order('name')->limit(100)->distinct();
echo $q->getQuery() . PHP_EOL;
echo print_r($q->getBindings()) . PHP_EOL;

SELECT `table2`.`age` AS a,`table1`.`name` AS n FROM table1,table2 WHERE `id`=:id AND `age` LIKE :age ORDER BY name LIMIT 100;
Array
(
    [id] => 13
    [age] => 3%
)

// insert
$q->newQuery()->insert(array('age'=>34, 'name'=>'John'))->table('table1,table2');
echo $q->getQuery() . PHP_EOL;
echo print_r($q->getBindings()) . PHP_EOL;

INSERT INTO table1(`age`,`name`) VALUES (:age,:name);
Array
(
    [age] => 34
    [name] => John
)

// update
$q->newQuery()->update(array('age'=>34, 'name'=>'John'))->table('table1')->where(array('$or'=>array('id'=>13, 'phone'=>'/+7916.*/')));
echo $q->getQuery() . PHP_EOL;
echo print_r($q->getBindings()) . PHP_EOL;

UPDATE table1 SET `age`=:age,`name`=:name WHERE `id`=:id OR `phone` LIKE :phone;
Array
(
    [age] => 34
    [name] => John
    [id] => 13
    [phone] => +7916%
)

// delete
$q->newQuery()->delete()->from('table3')->where(array('id'=>13));
echo $q->getQuery() . PHP_EOL;
echo print_r($q->getBindings()) . PHP_EOL;

DELETE FROM table3 WHERE `id`=:id;
Array
(
    [id] => 13
)

$q->from('table1')->select(array('table1.age'=>'a','table1.name'=>'n'))->order('n')->limit(100);
$q->join('tags','id','relid')->join('imgs','id','relid')->distinct();
$q->where(array('$or'=>array(array('tags.value'=>'three'),array('imgs.value'=>'image1.png'))));

$values = $q->getBindings()'