PHP code example of be / db4

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

    

be / db4 example snippets


"custom" => "mysql://username:[email protected]:3306/database#utf8",

'custom' => [
    'type'           => 'mysql',
    'hostname'       => '192.168.0.1',
    'database'       => 'database',
    'username'       => 'username',
    'password'       => 'password',
],


// 开启事务
$db->startTransaction();
try {
    // ...
    // 提交事务
    $db->commit();
} catch (\Exception $e) {
    // 回滚事务
    $db->rollback();
    // ...
}


// 返回用户信息对象
$user = $db->getObject('SELECT * FROM user WHERE id=1');

// 返回用户信息数组
$user = $db->getArray('SELECT * FROM user WHERE id=1');

// 返回符合条件的所有用户信息, 对象数组
$users = $db->getObjects('SELECT * FROM user WHERE age=18');

// 返回符合条件的所有用户信息, 二维数组
$users = $db->getArrays('SELECT * FROM user WHERE age=18');

// 返回单个值
$count = $db->getValue('SELECT COUNT(*) FROM user WHERE age=18');

// 返回多个值,数组
$names = $db->getValues('SELECT name FROM user WHERE age=18');

// 返回键值对, 带键名的数组(id 作为键名,name 作为值)
$idNameKeyValues = $db->getKeyValues('SELECT id, name FROM user WHERE age=18');

// 返回键值对, 带键名的数组(id 作为键名,用户信息数组 作为值)
$idUserKeyArrays = $db->getKeyArrays('SELECT id, name, age FROM user WHERE age=18');

// 返回键值对, 带键名的数组(id 作为键名,用户信息对像 作为值)
$idUserKeyObjects = $db->getKeyValues('SELECT id, name, age FROM user WHERE age=18');



// 以迭代器形式返回符合条件的所有用户信息,对象数组,用作处理大量数据,消耗较少内存
$users = $db->getYieldObjects('SELECT * FROM user WHERE age=18');

// 以迭代器形式返回符合条件的用户信息,二维数组,用作处理大量数据,消耗较少内存
$users = $db->getYieldArrays('SELECT * FROM user WHERE age=18');

$users = $db->getObjects('SELECT * FROM user WHERE age=?', [18]);
$users = $db->getObjects('SELECT * FROM user WHERE age>=? AND age<=?', [18, 25]);
$users = $db->getObjects('SELECT * FROM user WHERE name=?', ['abc']);
$users = $db->getObjects('SELECT * FROM user WHERE name=? AND age=?', ['abc', 18]);


$user = new stdClass();
$user->name = 'abc';
$user->age = 18;
$db->insert('user', $user);
$userId = $db->getLastInsertId();

$user = [];
$user['name'] = 'abc';
$user['age'] = 18;
$db->insert('user', $user);
$userId = $db->getLastInsertId();


$users = [];
$users[] = $user1;
$users[] = $user2;
// ...
$db->insertMany('user', $users);

$db->quickInsert('user', $user);
$db->quickInsertMany('user', $users);


$user->id = 1;
$db->update('user', $user);
$db->update('user', $user, 'id');

$db->replace('user', $user);
$db->replaceMany('user', $users);

$db->quickReplace('user', $user);
$db->quickReplaceMany('user', $users);


$search = "abc'abc";
$sql = 'SELECT * FROM ' . $db->quoteKey('user') . ' WHERE ' . $db->quoteKey('name') . '=' . $db->quoteValue($search)
$user = $db->getObject($sql);

// 对等SQL(MySQL): SELECT * FROM `user` WHERE `name` = 'abc\'abc'
// 对等SQL(Oracle): SELECT * FROM "user" WHERE "name" = 'abc\'\'abc'

$search = "abc'abc";
$sql = 'SELECT * FROM ' . $db->quoteKey('user') . ' WHERE ' . $db->quoteKey('name') . '=\'' . $db->escape($search) . '\''
$user = $db->getObject($sql);

// 对等SQL(MySQL): SELECT * FROM `user` WHERE `name` = 'abc\'abc'
// 对等SQL(Oracle): SELECT * FROM "user" WHERE "name" = 'abc\'\'abc'

$sql = 'UPDATE user SET age=20 WHERE name=\'abc\''
$db->query($sql);


$sql = 'UPDATE user SET age=? WHERE name=?'
$db->query($sql, [20, 'abc']);