PHP code example of quibble / query

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

    

quibble / query example snippets




$adapter = new class(/* connection params */) extends Quibble\Postgresql\Adapter
{
    use Quibble\Query\Buildable;
};




$query = $adapter->select('foo');




$query = $adapter->select('foo');
if ($someCondition) {
    $query = $query->where('bar = ?', $baz);
}
var_dump($query->fetchAll());



$query = new Quibble\Query\Select($pdo, $tableName);



$query = $adapter->select($adapter->select('foo')->where('1=1'));



$query = $adapter->select('foo, bar');
// SELECT * FROM foo, bar




use Quibble\Query\Join;

$query = $query->join(function (Join $join) {
    $join->inner('bar')
        ->on('bar.baz = foo.baz');
});




$query = $db->select('foo')->fields('bar');

$query = $query->asSubquery('foobar');

echo $db->select('baz')->fields($query);
// "SELECT (SELECT bar FROM foo) foobar FROM baz"



$query = $query->fields('foo', 'bar', 'baz AS buzz');




$query = $query->where('foo = ?', $bar);




$query = $query->orWhere('foo = ?', $bar);




$query = $query->where('foo = ? AND (bar = ? OR bar = ?)', $foo, $bar1, $bar2);
// ... (AND) foo = ? AND (bar = ? OR bar = ?)




$query = $query->where('bar = ?', 1)
    ->orWhere(function ($query) {
        $query->where('baz = ?', 2)
            ->where('buh = ?', 3);
    });
});

// SELECT * FROM some_table WHERE bar = 1 OR (baz = 2 AND buh = 3)




$query = $query->orderBy('foo ASC');




$query = $query->limit(10, 5);
// LIMIT 10 OFFSET 5




$query = $query->groupBy('foo, bar');




$query = $query = $query->groupBy('foo, bar')
    ->having('bar > ?', 42);




$query = $query->union($anotherQuery);




$query = $query->where('id IN (SELECT foo_id FROM bar WHERE bar_id = ?)', $bar_id);



$query = $query->where(function (Group $group) use ($arrayOfPossibleValues) {
    return $group->in('field', $arrayOfPossibleValues);
});



$results = $query->generate(PDO::FETCH_ASSOC);
foreach ($result as $results) {
    // ...
}



$result = (new Insert($pdo, 'tablename'))->execute(['foo' => 'bar']);
// INSERT INTO tablename (foo) VALUES (?) with bound parameter 'bar'



// Insert up to n rows:
$query->execute($array1, $array2, $arrayn);



$query = $pdo->insert('fooTable');



$query = new Quibble\Query\Update($pdo, $tableName);
// or, alternatively:
$query = $pdo->update($tableName);

$query->where('foo = ? AND bar = ?', $foo, $bar)
    ->execute(['baz' => $foobar]);
// UPDATE tableName SET baz = ? WHERE foo = ? AND bar = ?
// with bindings foo, bar and foobar.



$pdo->insert('foo')
    ->execute(['column' => ['NOW()']]);
$pdo->update('foo')
    ->where('id = ?', $id)
    ->execute(['column' => ['NOW()']]);



$query = new Quibble\Query\Delete($pdo, $tableName);
// or, alternatively:
$query = $pdo->delete($tableName);

$query->where('foo = ? AND bar = ?', $foo, $bar)
    ->execute();
// DELETE FROM tableName WHERE WHERE foo = ? AND bar = ?
// with bindings foo and bar.



class MySelect extends Quibble\Query\Select
{
    // ...whatever custom logic you need...
}

$adapter = new class(...connection...) extends Adapter {
    use Buildable;

    public function select(string|Select $table)
    {
        return new MySelect($this, $table);
    }
}