1. Go to this page and download the library: Download flatphp/lightdb 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/ */
flatphp / lightdb example snippets
use \Lightdb\DB;
// config 配置
$conf = array(
'dsn' => 'mysql:host=localhost;dbname=testdb',
'username' => 'root',
'password' => '123456',
'options' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']
);
// init 初始化
DB::init($conf);
// raw sql 原生sql使用
$sql = 'select * from test where id>?';
// static use 静态门面模式使用
$data = DB::fetchAllIndexed($sql, 2);
$sql = 'insert into test(aa, bb) values(?, ?)';
$res = DB::execute($sql, [1, 2]);
// connection instance use
$conn = DB::conn();
$sql = 'select * from channel where id>?';
$data = $conn->fetchAll($sql, 2);
// fetch all to classes array
// 数据实例化对象
$data = $conn->fetchAllTo('MyModel', $sql, 2);
// fetch row to class
$data = $conn->fetchRowTo('MyModel', $sql, 2);
use \Lightdb\DB;
$conf = array(
'dsn' => 'mysql:host=master;dbname=testdb',
'username' => 'root',
'password' => '123456',
'options' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'],
'slaves' => array(
[
'dsn' => 'mysql:host=slave1;dbname=testdb',
'username' => 'root',
'password' => '123456',
],
[
'dsn' => 'mysql:host=slave2;dbname=testdb',
'username' => 'root',
'password' => '123456',
]
)
);
DB::init($conf);
// write to master db
$sql = 'insert into test(aa) values(1)';
$res = DB::execute($sql);
// select from slave db
$sql = 'select * from test';
$data = DB::fetchAll($sql);
// force read from master
// 强制从主库读取
$sql = 'select * from test';
$data = DB::master()->fetchAll($sql);
use \Lightdb\DB;
$conf = array(
'db1' => array(...),
'db2' => array(...),
);
$sql = 'select * from test';
DB::conn('db1')->fetchRow($sql);
DB::conn('db2')->fetchPairs($sql);
use \Lightdb\DB;
$transaction = DB::transaction();
$transaction->run(function(){
// do something
}, function(){
// do something after commit
}, function(){
// do something after rollback
});
use Lightdb\DB;
use Lightdb\Raw;
DB::conn('other')->query()->table('users')->whereIn('name', ['peter', 'tom'])->update(array(
'class' => 1,
'point' => new Raw('point+1')
));
use Lightdb\DB;
// get delete sql
DB::query()->where('id=?', 10)->delete();
// execute
$query = DB::conn('another')->query();
$query->table('users')->whereNotIn('id', [1,2,3])->delete();
print_r($query->getLog());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.