PHP code example of manekshms / db2util

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

    

manekshms / db2util example snippets


    $config = [];
    $config['dsn'] = 'ibm:dbname';
    $config['username'] = 'db2admin';
    $config['password'] = 'db2admin';
    $db2Util = new DB2Util\DB2Util($config);
    $db2Util->connect();

$db2Util->getQueryBuilder()->table('table_name')->where('name' , 'bob')->get();

$db2Util->getQueryBuilder()->table('table_name')->where(['name' => 'bob', 'age' => 40])->get();

$db2Util->getQueryBuilder()->table('table_name')->where('AGE', '>=', 40)->get();

$db2Util->getQueryBuilder()->table('user')->where([ ['AGE', '>=', 40], ['NAME', '=', 'bob'] ])->get(); 

$db2Util->getQueryBuilder()->table('user')->where([ ['AGE', '>=', 40], ['NAME', 'bob'] ])->get(); 

$db2Util->getQueryBuilder()->table('user')->where('AGE', 'in', [40, 30])->get();

$db2Util->getQueryBuilder()->table('user')->limit(3)->get(); 

$db2Util->getQueryBuilder()->table('user')->limit(2)->offset(2)->get();  

$db2Util->getQueryBuilder()->table('user')->where('name', 'like', '%doe%')->get();  

    $db2Util->getQueryBuilder()
            ->table('user_PRODUCT AS USER_PRODUCT')
            ->select(['USER_PRODUCT.ID', 'USER.NAME', 'PRODUCT.PRODUCT_NAME', 'PRODUCT.PRICE'])
            ->join('user AS USER ', 'USER_PRODUCT.USER_ID', '=', 'USER.ID')
            ->join('PHPUNIT_TEST_PRODUCT AS PRODUCT', 'USER_PRODUCT.PRODUCT_ID', '=', 'PRODUCT.ID')
            ->get();

    $db2Util->getQueryBuilder()
                    ->table('user')
                    ->where('AGE', '>=', 40)
                    ->where(function($query){
                        $query->where('EMAIL', 'like', 'm%')
                            ->orWhere('NAME', 'in', ['bob', 'boo']);
                        })
                    ->get()

    $firstQuery = $db2Util->getQueryBuilder()->table('user')->where('AGE','40');
    $unionResult = $db2Util->getQueryBuilder()->table('user')->where('NAME','like', 'b%')->unionAll($firstQuery)->get();

$result = $db2Util->getQueryBuilder()->table('user')->select(["count(*) as cnt", "age"])->groupBy('age')->having('count(*)', '>', 1)->get();

$db2Util->getQueryBuilder()->table('user')->whereIn('first_name', ['john', 'jack'])->get();

$db2Util->getQueryBuilder()->table('user')->whereNotIn('first_name', ['john', 'jack'])->get();

$db2Util->getQueryBuilder()->table('name')->pluck('age');

$db2Util->getQueryBuilder()->table('table name')->count();

$db2Util->getQueryBuilder()->table('table name')->max('age');

$db2Util->getQueryBuilder()->table('table name')->min('age');

$data =  [ 'NAME' => 'bob', 'AGE' => 40,'EMAIL' => '[email protected]', 'ADDRESS' =>  'bob address goes here'];
$db2Util->getQueryBuilder()->table('user')->insert($data);

$data = [
['NAME' => 'mikee', 'AGE' => 54,'EMAIL' => '[email protected]', 'ADDRESS' =>  'mikee address goes here'],
['NAME' => 'mark', 'AGE' => 64,'EMAIL' => '[email protected]', 'ADDRESS' =>  'mark address goes here'],
['NAME' => 'momo', 'AGE' => 34,'EMAIL' => '[email protected]', 'ADDRESS' =>  'momo address goes here']
];
$db2Util->getQueryBuilder()->table('user')->insert($data);

$db2Util->getQueryBuilder()->table('user')->update(['EMAIL' => '[email protected]']);

$db2Util->getQueryBuilder()->table('user')->where([['age', 40], ['NAME', 'bob']])->update(['EMAIL' => '[email protected]']);

$db2Util->getQueryBuilder()->table('user')->where('ID', '1')->increment('age', 2);

$db2Util->getQueryBuilder()->table('user')->where('ID', '1')->decrement('age');

$db2Util->getQueryBuilder()->table('user')->delete();

$db2Util->getQueryBuilder()->table('user')->where('age', '!=', '40')->delete();

$db2Util->getConnection()->getLastSQLQuery(true);

array(3) {
  'query' => " SELECT * FROM user WHERE AGE = ?  ",
  'params' =>[
    40
  ],
  'processed_query' => " SELECT * FROM user WHERE AGE = '40'  "
}

    $sql = " INSERT INTO USER (name, age, country) VALUES ( ?, ?, ? ) ";
    $params = [
        'bob',
        20,
        'India'
    ];
   $db2Util->getConnection()->executeQuery($sql, $params); 


   $sql = " SELECT * FROM USER WHERE ID = ? ";
   $params = [20];
   $stmt = $db2Util->getConnection()->query($sql, $params); 
   $stmt->fetchAll();

sql
SELECT count(*) as cnt, age FROM user GROUP BY age HAVING count(*) >  '1'