PHP code example of orinfy / db

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

    

orinfy / db example snippets


\Odb\DB::loadConfig("DBConf.php");

return [
    'default' => [     // default must exists
        'driver' => 'mysql',   // pgsql(postgresql)
        'host' => '127.0.0.1',
        'port' => '3306',
        'username' => '',
        'password' => '',
        'dbname' => '',
        'charset' => 'utf8',
        'pconnect' => false,
        'time_out' => 3,
        'prefix' => '',
        'throw_exception' => true
    ]
];

\Odb\DB::connect()->getDBVersion();

\Odb\DB::connect('default')->query(...)->get();

\Odb\DB::table('user')->insert(['name' => 'jack', 'age' => 24]);

\Odb\DB::table('user')->insert([['name' => 'linda', 'age' => 21], ['name' => 'bob', 'age' => 24]]);

$id = \Odb\DB::table('user')->insertGetId(['name' => 'ailan', 'age' => 21]);

\Odb\DB::table('user')->allow('name', 'age')->insert(['name' => 'jack', 'age' => 24, 'job' => 'programmer']);

echo \Odb\DB::table('user')->buildInsert(['name' => 'jack', 'age' => 24])->getSQL();

echo \Odb\DB::table('user')->buildInsert(['name' => 'jack', 'age' => 24])->getRSql();

\Odb\DB::table('user')->where('id', 1)->update(['name' => 'remi']);

\Odb\DB::table('scores')->where('id', 10)->increment('score');
\Odb\DB::table('scores')->where('id', '=', 10)->increment('score', 5);
\Odb\DB::table('scores')->where('id', '=', 10)->increment([['score', 1], ['level', 9]]);

\Odb\DB::table('scores')->where('id', '=', 10)->decrement('score');
\Odb\DB::table('scores')->where('id', '=', 10)->decrement('score', 2);
\Odb\DB::table('scores')->where('id', '=', 10)->decrement([['score', 2], ['level', 1]]);

\Odb\DB::table('logs')->where([['id', '>', 9], ['level', '<', 2]])->delete();

$rows = \Odb\DB::table('user')->get();

$user = \Odb\DB::table('user')->first();

$userNames = \Odb\DB::table("users")->pluck('username');
$userNames = \Odb\DB::table("users")->pluck('username', 'id');

echo \Odb\DB::table('users')->where('id', '=', 2)->value('username');  // 'jack'

\Odb\DB::table('user')->max('age');
\Odb\DB::table('user')->min('age');
\Odb\DB::table('user')->sum('age');
\Odb\DB::table('user')->count();
\Odb\DB::table('user')->avg('age');

\Odb\DB::table('user')->where('id', 10)->where('level', '=', 5)->get();
\Odb\DB::table('user')->where([['id', '>', 10], ['level', '=', 5]])->orWhere('status', '=', 0)->get();
\Odb\DB::table('user')->where([['id', '>', 10], ['level', '=', 5]])->orWhere('status', '=', 0)
    ->orWhere(function ($query) {
        $query->whereNull('username');
    })->get();

whereNull('username');
whereNotNull('username');
whereIn('id', [1, 2, 3]);
whereNotIn('id', [1, 2, 3]);
whereBetween('id', [1, 9]);
whereNotBetween('id', [1, 9]);
whereColumn('id', '>', 'parent_id');
where('`username`=? and `age`>?', ['job', 23]);
where('username', '=', 'job')->where('age', '>', 23);
where([['username', '=', 'job'], ['age', '>', 23]]);
whereRaw('`id`>? and `status`=?', [10, 1]);

\Odb\DB::table('users')->select('id', 'username', 'age')->get();
\Odb\DB::table('users')->select(['id', 'username', 'age'])->get();
\Odb\DB::table('users')->select(['id', 'username', 'age', 'sum(score) as total'])->get();

\Odb\DB::table('user as u')->join('article as a', 'u.id', '=', 'a.uid')->select('u.*', 'a.commend')->get();

\Odb\DB::table('user')->leftJoin('role', 'user.role_id', '=', 'role.id')->rightJoin('posts', 'uid', '=', 'user.id');
\Odb\DB::table('user')->join('role', 'test_user.role_id=test_role.id and test_role.status>?', [1]);

\Odb\DB::table('user')->orderBy('id', 'desc')->get();
\Odb\DB::table('user')->orderBy('id', 'asc')->get();
\Odb\DB::table('user')->select('count(id) as num')->groupBy('team_id')->having('num', '>', 2)->get();
\Odb\DB::table('user')->select('count(id) as num')->groupBy('team_id')->having('`num`>?', [2])->get();
\Odb\DB::table('user')->limit(2)->get();
\Odb\DB::table('user')->limit(5, 2)->get();

\Odb\DB::beginTrans();
\Odb\DB::inTrans();
\Odb\DB::rollBack();
\Odb\DB::commit();

\Odb\DB::exec('delete from test_user');
\Odb\DB::query('select * from test_user')->get();
\Odb\DB::prepare('select * from test_user where id>?')->execute([10])->get();
\Odb\DB::prepare('update test_user set username=?')->execute(['aiwa'])->rowCount();
\Odb\DB::prepare('insert into test_user (username) values(?)')->execute(['aiwa'])->lastInsertId();
sql
'insert into `user` (`name`,`age`) values (?,?)'