PHP code example of walnut / lib_dbquery

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

    

walnut / lib_dbquery example snippets


$query = new FixedQuery("SELECT * FROM users WHERE is_active = 1");
$result = $query->execute($queryExecutor);
$result->all(); //all active users

$query = new PreparedQuery("SELECT * FROM users WHERE id = :id", ['id']);
$result = $query->execute($queryExecutor, ['id' => 5]);
$result->first(); //user with id 5 (or null if it does not exist)

$query = new Placeholder("SELECT * FROM users WHERE name LIKE **__name__**", ['name']);
$result = $query->execute($queryExecutor, null, ['name' => '%john%']);
$result->all(); //all user with name including "john"

$bag = new ListResultBag([
    1 => ['id' => 1, 'name' => 'Name 1'],
    2 => ['id' => 2, 'name' => 'Name 2'],
    3 => ['id' => 3, 'name' => 'Name 3'],
]);
$bag->all(); //3 items
$bag->withKey(1); //['id' => 1, 'name' => 'Name 1']
$bag->withKey(5); //null
$bag->collect('name'); //['Name 1', 'Name 2', 'Name 3']

$bag = new TreeDataResultBag([
    1 => [
        ['id' => 1, 'name' => 'Name 1'],
        ['id' => 2, 'name' => 'Name 2']
    ],
    3 => [
        ['id' => 3, 'name' => 'Name 3']
    ]
]);
$bag->all(); //array(2 items, 1 item)
$bag->withKey(1); //array(2 items)
$bag->withKey(5); //array(0 items)
$bag->collect('name'); //['Name 1', 'Name 2', 'Name 3']