PHP code example of memran / marwadb

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

    

memran / marwadb example snippets



use MarwaDB\DB;
$config = [
    'default'=>
        [
           'driver' => "mysql",
           'host' => "localhost",
           'port' => 3306,
           'database' => "test",
           'username' => "root",
           'password' => "1234",
           'charset' => "utf8mb4",
        ],
    'write'=>
        [
           'driver' => "mysql",
           'host' => "localhost",
           'port' => 3306,
           'database' => "test",
           'username' => "root",
           'password' => "1234",
           'charset' => "utf8mb4",
        ],
    'read'=>
        [
           'driver' => "mysql",
           'host' => "localhost",
           'port' => 3306,
           'database' => "test",
           'username' => "root",
           'password' => "1234",
           'charset' => "utf8mb4",
        ]
];
$db = new DB($config);

    $result = $db->raw('SELECT * FROM system WHERE id = ?',[1]);
    dump($result)

    $result = $db->rawQuery('SELECT * FROM system WHERE id = ?',[1]);
    dump($result)

    dump("Total Rows Returned >>> ".$db->rows());

    $db->status();

    $result=$db->connection('sqlSrv')->rawQuery('SELECT  *  FROM users WHERE id = ?',  [1]);
    dump($result);

    $result=$db->connection('sqlSrv')->setFetchMode('array')->rawQuery('SELECT  *  FROM users WHERE id = ?',  [1]);
    dump($result);

    $db->transaction(function($db){
       $db->rawQuery('DELETE  FROM users WHERE id = ?',  [4]);
       dump($db->rows());
    });

    $result =  $db->select('SELECT  *  FROM users');
    dump($result);

    $result=$db->select('SELECT  *  FROM users WHERE id = ?',  [1]);
    dump($result);

    $result =  $db->raw("SELECT  *  FROM users WHERE id = :id",  ['id'  =>  '1']);
    dump($result);

    dump($db->getDriver());

$db->table('users')->get();

$db->table('users')->where('name', 'Marwa')->first()->get();

$db->table('roles')->select(['title', 'name'])->get();

$users = $db->table('users')->count()->get();
$price = $db->table('orders')->max('price')->get();
$price = $db->table('orders')->avg('price')->get();
$price = $db->table('orders')->min('price')->get();

$users = $db->table('users')->select(['name', 'email as user_email'])->get();

$users = $db->table('users')->distinct()->get();

$result =$db->table('users')->addSelect('age')->get();

$users = $db->table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

$users = $db->table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

$users = $db->table('users')
            ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

$first = $db->table('users')
            ->whereNull('first_name');

$users = $db->table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();

$users = $db->table('users')->where('votes', '=', 100)->get();

$users = $db->table('users')
                ->where('votes', '>=', 100)
                ->get();

$users = $db->table('users')
                ->where('votes', '<>', 100)
                ->get();

$users = $db->table('users')
                ->where('name', 'like', 'T%')
                ->get();

$users = $db->table('users')
                    ->where('votes', '>', 100)
                    ->orWhere('name', 'John')
                    ->get();

$users = $db->table('users')
           ->whereBetween('votes', [1, 100])
           ->get();

$users = $db->table('users')
                    ->whereNotBetween('votes', [1, 100])
                    ->get();

$users = $db->table('users')
                    ->whereIn('id', [1, 2, 3])
                    ->get();

$users = $db->table('users')
                    ->whereNotIn('id', [1, 2, 3])
                    ->get();

$users = $db->table('users')
                    ->whereNull('updated_at')
                    ->get();

$users = $db->table('users')
                    ->whereNotNull('updated_at')
                    ->get();

$users = $db->table('users')
                ->whereDate('created_at', '2016-12-31')
                ->get();

$users = $db->table('users')
                ->whereMonth('created_at', '12')
                ->get();

$users = $db->table('users')
                ->whereDay('created_at', '31')
                ->get();

$users = $db->table('users')
                ->whereYear('created_at', '2016')
                ->get();

$users = $db->table('users')
                ->whereTime('created_at', '=', '11:20:45')
                ->get();

$users = $db->table('users')
           ->whereExists(function ($query) {
               $query->select([1])
                     ->from('users')
                     ->where('id', '=','1');
           })
           ->get();

$users = $db->table('users')
                ->orderBy('name', 'desc')
                ->get();

$user = $db->table('users')
                ->latest()
                ->first()
                ->get();

$randomUser = $db->table('users')
                ->inRandomOrder()
                ->first()
                ->get();

$users = $db->table('users')
                ->groupBy('account_id')
                ->having('account_id', '>', 100)
                ->get();

$users = $db->table('users')->skip(10)->take(5)->get();

$db->table('users')->insert(
    ['email' => '[email protected]', 'active' => 0]
);

$db->table('users')->insert([
    ['email' => '[email protected]', 'active' => 0],
    ['email' => '[email protected]', 'active' => 1]
]);

$result= $db->table('users')
              ->where('id', 1)
              ->update(['active' => 1]);

$db->table('users')
    ->updateOrInsert(
        ['email' => '[email protected]', 'name' => 'Marwa'], //data for update
        ['active' => '1] // data for insert
    );

$db->table('users')->delete();

$db->table('users')->where('active', '=', 0)->delete();

//It will debug and die
$db->table('users')->where('active', '=', 1)->dd();
//it will only debug
$db->table('users')->where('active', '=', 1)->dump();

$db->enableQueryLog();
$db->table('users')->where('active', '=', 1)->get();
dump($db->getQueryLog());

dump($db->table('users')->where('active', '=', 1)->toSql());