PHP code example of mix / database

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

    

mix / database example snippets


$db = new Mix\Database\Database('mysql:host=127.0.0.1;port=3306;charset=utf8;dbname=test', 'username', 'password');

$db->insert('users', [
    'name' => 'foo',
    'balance' => 0,
]);

$db->table('users')->where('id = ?', 1)->first();

$db->table('users')->where('id = ?', 1)->update('name', 'foo1');

$db->table('users')->where('id = ?', 1)->delete();

$maxOpen = 50;        // 最大开启连接数
$maxIdle = 20;        // 最大闲置连接数
$maxLifetime = 3600;  // 连接的最长生命周期
$waitTimeout = 0.0;   // 从池获取连接等待的时间, 0为一直等待
$db->startPool($maxOpen, $maxIdle, $maxLifetime, $waitTimeout);
Swoole\Runtime::enableCoroutine(); // 必须放到最后,防止触发协程调度导致异常

$db->poolStats(); // array, fields: total, idle, active

$data = [
    'name' => 'foo',
    'balance' => 0,
];
$db->insert('users', $data);

$data = [
    'name' => 'foo',
    'balance' => 0,
];
$insertId = $db->insert('users', $data)->lastInsertId();

$data = [
    'name' => 'foo',
    'balance' => 0,
];
$db->insert('users', $data, 'REPLACE INTO');

$data = [
    [
        'name' => 'foo',
        'balance' => 0,
    ],
    [
        'name' => 'foo1',
        'balance' => 0,
    ]
];
$db->batchInsert('users', $data);

$data = [
    'name' => 'foo',
    'balance' => 0,
    'add_time' => new Mix\Database\Expr('CURRENT_TIMESTAMP()'),
];
$db->insert('users', $data);

$db->table('users')->where('id = ? AND name = ?', 1, 'foo')->get();

$db->table('users')->where('id = ?', 1)->where('name = ?', 'foo')->get();

$db->table('users')->where('id = ? OR id = ?', 1, 2)->get();

$db->table('users')->where('id = ?', 1)->or('id = ?', 2)->get();

$db->table('users')->where('id IN (?)', [1, 2])->get();

$db->table('users')->where('id NOT IN (?)', [1, 2])->get();

$db->table('users')->select('id, name')->get();

$db->table('users')->select('id', 'name')->get();

$db->table('users')->select('name AS n')->get();

$db->table('users')->order('id', 'desc')->get();

$db->table('users')->order('id', 'desc')->order('name', 'asc')->get();

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

$db->table('users')->offset(10)->limit(5)->get();

$db->table('news')->select('uid, COUNT(*) AS total')->group('uid')->having('COUNT(*) > ?', 0)->get();

$db->table('news')->select('uid, COUNT(*) AS total')->group('uid')->having('COUNT(*) > ? AND COUNT(*) < ?', 0, 10)->get();

$db->table('news AS n')->select('n.*, u.name')->join('users AS u', 'n.uid = u.id')->get();

$db->table('news AS n')->select('n.*, u.name')->leftJoin('users AS u', 'n.uid = u.id AND u.balance > ?', 10)->get();

$db->table('users')->where('id = ?', 1)->update('name', 'foo1');

$rowsAffected = $db->table('users')->where('id = ?', 1)->update('name', 'foo1')->rowCount();

$data = [
    'name' => 'foo1',
    'balance' => 100,
];
$db->table('users')->where('id = ?', 1)->updates($data);

$db->table('users')->where('id = ?', 1)->update('balance', new Mix\Database\Expr('balance + ?', 1));

$data = [
    'balance' => new Mix\Database\Expr('balance + ?', 1),
];
$db->table('users')->where('id = ?', 1)->updates($data);

$db->table('users')->where('id = ?', 1)->update('add_time', new Mix\Database\Expr('CURRENT_TIMESTAMP()'));

$data = [
    'add_time' => new Mix\Database\Expr('CURRENT_TIMESTAMP()'),
];
$db->table('users')->where('id = ?', 1)->updates($data);

$db->table('users')->where('id = ?', 1)->delete();

$rowsAffected = $db->table('users')->where('id = ?', 1)->delete()->rowCount();

$db->raw('SELECT * FROM users WHERE id = ?', 1)->first();

$db->exec('DELETE FROM users WHERE id = ?', 1)->rowCount();

$tx = $db->beginTransaction();
try {
    $data = [
        'name' => 'foo',
        'balance' => 0,
    ];
    $tx->insert('users', $data);
    $tx->commit();
} catch (\Throwable $ex) {
    $tx->rollback();
    throw $ex;
}

$db->transaction(function (Mix\Database\Transaction $tx) {
    $data = [
        'name' => 'foo',
        'balance' => 0,
    ];
    $tx->insert('users', $data);
});

$db->debug(function (Mix\Database\ConnectionInterface $conn) {
        var_dump($conn->queryLog()); // array, fields: time, sql, bindings
    })
    ->table('users')
    ->where('id = ?', 1)
    ->get();

$db->setLogger($logger);

interface LoggerInterface
{
    public function trace(float $time, string $sql, array $bindings, int $rowCount, ?\Throwable $exception): void;
}