PHP code example of elephpant / light-query-builder
1. Go to this page and download the library: Download elephpant/light-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/ */
elephpant / light-query-builder example snippets
lePHPant\LightQueryBuilder;
$lightQB = (new LightQueryBuilder())::setTable("users")->setFetchClass(stdClass::class);
$select = $lightQB->select();
//Returns 'SELECT * FROM users'
$selectWithColumns = $lightQB->select("fullname, email");
//Returns 'SELECT fullname, email FROM users';
$where = $select->where("gender = :g", "g=male");
//Returns 'SELECT * FROM users WHERE gender = :g' -> working with bind param in PDO
$where->and("id >=2")->or("id <= 10");
//Returns 'SELECT * FROM users WHERE gender = :g AND id >= 2 OR id <= 10'
$between = $select->where("DATE(birth)")->between("'2020-03-17'", "'2020-04-01'");
//Returns 'SELECT * FROM users WHERE DATE(birth) BETWEEN '2020-03-17' AND '2020-04-01''
$lightQB->join("fullname", "clients", "client.user=users.id", LightQueryBuilder::INNER_JOIN);
//Returns 'SELECT fullname FROM users INNER JOIN clients ON client.user=users.id'
$lightQB->join("fullname", "clients", "client.user=users.id", LightQueryBuilder::RIGHT_JOIN);
//Returns 'SELECT fullname FROM users RIGHT JOIN clients ON client.user=users.id'
//[...]
$select->limit(3)->offset(2);
//Returns 'SELECT * FROM users LIMIT 3 OFFSET 2'
$select->count();
//Returns all RowCounts of the consult
$lightQB->match("fullname, email", "Pedro", true);
//Returns the result of alll users that match with the fullname or email with 'Pedro'.
$lightQB->toQuery("
SELECT * FROM my_table
WHERE id = 2
")->limit(2)->offset(1);
$create = $lightQB->create(array(...));
$select->get(); //Like that it'll bring only one result (first) [object]
$select->get(true); //Like that it'll bring all results [array]
$update = $lightQB->update(array(...), "WHERE id = :id", "id=2");