PHP code example of php-objects / query-builder

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

    

php-objects / query-builder example snippets


$fields = array('u.name AS name', 'r.name AS role');

// Selecting via factory
$select = PO\QueryBuilder::factorySelect($fields);

// Selecting via the select method
$select = PO\QueryBuilder::factorySelect()
    ->select($fields);

// or alternatively
$select = new PO\QueryBuilder\Statements\Select();
$select->select($fields);

// From
$select->from('users u');

// Adding joins
$select->innerJoin('roles r', 'u.id = r.user_id');

$select->toSql();

// SELECT u.name AS name, r.name AS role 
// FROM users u INNER JOIN roles r ON u.idi = r.user_id

// Using the factory
$insert = PO\QueryBuilder::insert();

// Or alternatively
$insert = new PO\QueryBuilder\Statements\Insert();

$insert->into('users')->values(array(
    'name'  => 'Jon Doe',
    'email' => '[email protected]'
));

$insert->toSql();

// INSERT INTO users (name, email) VALUES ('Jon Doe', '[email protected]');

$update = PO\QueryBuilder::update('users');

// or
$update = new PO\QueryBuilder\Statements\Update;
$update->table('users');

// setting values and conditions

$update->set(array(
        'enabled' => 1
    ))->where('email', ':email');

$update->toSql(array(
    'email' => '[email protected]'
));

// UPDATE users SET enabled = 1 WHERE email = '[email protected]'


// method signature
$query->where($field, $value, $operator);

// or
$query->where($condition);

// or
$query->where(array(
    array($field, $value, $operator),
    array($condition),
));

// Below some valid examples:

$query->where('email', '[email protected]');
// WHERE email = '[email protected]'

$query->where('email', '[email protected]', '<>');
// WHERE email <> "[email protected]"

$query->where('email', '%@google.com', 'LIKE');
// WHERE email <> "[email protected]"

$query->where('age', 20);
// WHERE age = 20

$query->where('code', 001);
// WHERE code = 001

$query->where('code', array('value' => '001'));
// WHERE code = '001'

$query->where('(code = 1 OR code = 2)'));
// WHERE (code = 1 OR code = 2)

// multiple conditioins, one method call
$query->where(array(
    array('email', '[email protected]', '<>'),
    array('email', '%@google.com', 'LIKE'),
    array('age', 20),
    array('(code = 1 OR code = 2)'),
    array('hash', array('value' => 'SOMEFUNCTION()')),
));

// WHERE condition 1 AND condition 2..

$query->orderBy('name DESC');
// or
$query->orderBy(array('name DESC', 'age ASC'));

$query->groupBy('a, b, c');
// or
$query->groupBy(array('a', 'b', 'b'));

$query->limit(2);
$query->limit(2, 1);

$insert->into('users')->values(array(
    'name'  => ':name',
    'email' => ':email'
));

$insert->toSql(array(
    'name'  => 'Jon Doe',
    'email' => '[email protected]'
));

// INSERT INTO users (name, email) VALUES ('Jon Doe', '[email protected]');