PHP code example of safronik / db-wrapper

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

    

safronik / db-wrapper example snippets


$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'   => 'pdo',
        'username' => 'root',
        'password' => 'root',
        'hostname' => 'localhost', // or could be a container name if you are using Docker 
    ])
);

global $some_pdo_connection_object; // should be an instanceof \PDO

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'     => 'pdo',
        'connection' => $some_pdo_connection_object,
    ])
);

global $some_pdo_connection_object; // should be an instanceof \PDO

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'connection' => $some_pdo_connection_object,
    ])
);

$global $wpdb;

$db = DB::getInstance(
    new \Safronik\DB\DBConfig([
        'driver'     => 'wpdb',
        'connection' => $wpdb,
    ])
);

$rows_affected = $db->query( 'DELETE FROM some_table LIMIT 10' );

$query_result = $db
    ->query( 'SELECT * FROM some_table' ) // Query already executed at this point
    ->fetchAll();                         // Fetching the result

$db
    ->select('users')
    ->orderBy('register_date', 'desc')
    ->limit(10)
    ->run();

$values = [
    'some'    => 'thing',
    'another' => 'stuff',
]

$db
    ->insert( 'some_entity' )
    ->columns( array_keys( $values ) )
    ->values( $values )
    ->onDuplicateKey( 'update', $values )
    ->run();

$db
    ->update( 'options' )
    ->set( [ 'option_value' => $value ] )
    ->where([
        'option_name' => $option,
        'affiliation' => $group,
    ])
    ->and([ 'something' => 'different'])
    ->or( ['another' => 'example'])
    ->run()

$db
    ->update( 'options' )
    ->set( [ 'option_value' => $value ] )
    ->where([
        'option_name' => $option,
        'affiliation' => $group,
    ])
    ->run()

$db
    ->select('users')
    ->join(
        [
            [ 'table_name_or_alias', 'id' ],
            '=', // <=> | != | > | < | >= | <=
            [ 'table_name_of_second_table_or_alias_2', 'some_id' ],
        ],
        'left',                                            // right | left | inner
        ['some_id', 'another_column', 'some_other_column'] // list of columns you want to join
    )
    ->limit(10)
    ->run();

$db
    ->select( 'cte' )
    ->with(
        $db
            ->cte( 'cte' )
            ->anchor( 'SELECT * FROM some_table WHERE id = 1' )
            ->recursive( '
                SELECT some_table.* 
                FROM some_table, cte
                WHERE some_table.parent = cte.id'
            )
        )
    )
    ->run();

$db
    ->select( 'cte' )
    ->with(
        $db
            ->cte( 'cte' )
            ->anchor(
                $db
                    ->select( $block_table )
                    ->where( [ 'id' => 1 ] )
            )
            ->recursive(
                $db
                    ->select( [ $block_table, 'cte' ])
                    ->columns( '*', $block_table )
                    ->where( [ [ [ $block_table, 'parent' ], '=', [ 'cte', 'id' ] ] ] )
        )
    )
    ->run();

$db->isTableExists( 'table_name');

$db->dropTable( 'table_name');