PHP code example of qingbing / php-database

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

    

qingbing / php-database example snippets


// 获取数据库的服务信息
$serverInfo = $db->getServerInfo();
var_dump($serverInfo);
// 获取数据库的版本信息
$serverVersion = $db->getServerVersion();
var_dump($serverVersion);
// 获取驱动的版本信息
$clientVersion = $db->getClientVersion();
var_dump($clientVersion);

// sql 插入语句范例
$sql = "INSERT INTO `{{stu}}` (`name`,`sex`) VALUES (:name,:sex)";
$res = $db->insertBySql($sql, [
    ':name' => 'name_12',
    ':sex' => 'sex_12',
]);
var_dump($res);
// sql 更新语句范例
$sql = "UPDATE `{{stu}}` SET `sex`=:sex WHERE id>=:bid AND id<=:eid";
$res = $db->updateBySql($sql, [
    ':sex' => 12345678,
    ':bid' => 2,
    ':eid' => 4,
]);
var_dump($res);
// sql 删除语句范例
$sql = "DELETE FROM `{{stu}}` WHERE id>=:bid AND id<=:eid";
$res = $db->deleteBySql($sql, [
    ':bid' => 2,
    ':eid' => 4,
]);
var_dump($res);
// Sql 查询符合条件的记录数
$sql = "SELECT * FROM `{{stu}}`";
$count = $db->countBySql($sql);
var_dump($count);

$sql = "SELECT * FROM `{{stu}}`";
$row = $db->findBySql($sql);
var_dump($row);

$sql = "SELECT * FROM `{{stu}}`";
$records = $db->findAllBySql($sql);
var_dump($records);

// 单记录数组插入操作
$res = $db->insert('{{stu}}', [
    'name' => 'name_1',
    'sex' => 'sex_1',
]);
var_dump($res);
// 多记录数组插入操作
$res = $db->insertData('{{stu}}', [
    ['name' => 'name_2', 'sex' => 'sex_2',],
    ['name' => 'name_3', 'sex' => 'sex_3',],
]);
var_dump($res);
// 获取插入的最后一次的insertID
$lastId = $db->getLastInsertId();
var_dump($lastId);

// 更新数组操作
$res = $db->update('{{stu}}', ['sex' => '1234511'], 'id>=:bid AND id<=:eid', [
    ':bid' => 2,
    ':eid' => 4,
]);
var_dump($res);
// 删除 build 操作
$res = $db->delete('{{stu}}', 'id>=:bid AND id<=:eid', [
    ':bid' => 2,
    ':eid' => 4,
]);
var_dump($res);
// 初始化 条件
$criteria = new Criteria();
$criteria->setTable('{{stu}}')
    ->addWhere('id>:startId', [
        ':startId' => 2
    ]);
// 查询符合条件的记录数
$count = $db->count($criteria);
var_dump($count);
// 查询符合条件的首条记录
$row = $db->find($criteria);
var_dump($row);
// 查询符合条件的全部记录
$res = $db->findAll($criteria);
var_dump($res);

// 插入
$res = $db->getInsertBuilder()
    ->setTable('{{stu}}')
    ->setColumns([
        'name' => 'name-builder-single',
        'sex' => 'builder',
    ])
    ->execute();
var_dump($res);

// 批量插入
$res = $db->getInsertBuilder()
    ->setTable('{{stu}}')
    ->setMultiFields(['name', 'sex'])
    ->setMultiData([
        [
            'name' => 'name-builder-multi_1',
            'sex' => 'builder',
        ],
        [
            'name' => 'name-builder-multi_2',
            'sex' => 'builder',
        ],
    ])
    ->execute();
var_dump($res);

$lastId = $db->getLastInsertId();

// 更新
$res = $db->getUpdateBuilder()
    ->setTable('{{stu}}')
    ->setColumns([
        'sex' => 'update'
    ])
    ->addWhere('id=:id', [':id' => $lastId])
    ->execute();
var_dump($res);

// 删除
$res = $db->getDeleteBuilder()
    ->setTable('{{stu}}')
    ->addWhere('id=:id', [':id' => 2])
    ->execute();
var_dump($res);

// 查询
$res = $db->getFindBuilder()
    ->setTable('{{stu}}')
    ->setSelect('id, name')
    ->addWhere('id=:id', [':id' => $lastId])
    ->queryAll();
var_dump($res);

$transaction = $db->beginTransaction();

// 更新
$res = $db->getUpdateBuilder()
    ->setTable('{{stu}}')
    ->setColumns([
        'sex' => 'update111'
    ])
    ->addWhere('id=:id', [':id' => 12])
    ->execute();
var_dump($res);

//        $transaction->commit();
$transaction->rollback();

$criteria = (new Criteria())
    ->setTable('{{stu}}')
    ->addWhere('t.id>:startId', [
        ':startId' => 2
    ]);

$res = $db->pagination($criteria, [], 2, 3);
var_dump($res);


var_dump(1111);
$sql = "SELECT * FROM `{{stu}}` t WHERE t.id>:startId";
$res = $db->pagination($sql, [
    ':startId' => 2
], 2);
var_dump($res);


// 获取表结构
$schema = $db->getTable('{{stu}}');
var_dump($schema);