PHP code example of guanhui07 / database

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

    

guanhui07 / database example snippets


/**
 * 创建配置
 */
(new \Guanhui07\SwooleDatabase\PDOConfig())->
withDriver('mysql')-> // 驱动类型
withHost('127.0.0.1')-> // 主机地址
withDbname('test')-> // 数据库名
withUsername('root')-> // 用户名
withPassword('123456')-> // 密码
withCharset('utf8mb4')-> // 字符集编码
setConfig('default'); // 设置全局访问(默认为default)

\Guanhui07\SwooleDatabase\PoolManager::addPool(64,'default'); // 设置指定连接池尺寸(连接名称默认为 default)

/**
 * 开启协程(如果框架内已经开启可忽略)
 */
\Swoole\Runtime::enableCoroutine();
/**
 * 协程化IO钩子
 */
\Swoole\Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL]);
/**
* 记录开始时间
 */
$s = microtime(true);
\Swoole\Coroutine\run(function () {
    /**
     * 循环创建协程(模拟HTTP 请求 执行任务)
     */
    for ($i = 0; $i < 20; $i++) {
        \Swoole\Coroutine::create(function () {
            /**
             * 创建表
             */
//            \Guanhui07\SwooleDatabase\Manager::schema()->create('test',function(\Illuminate\Database\Schema\Blueprint $table){
//                $table->increments('id');
//                $table->string('name')->nullable()->default(1);
//                $table->timestamps();
//            });
            /**
             * 模型查询
             */
//            $lists = \Guanhui07\SwooleDatabase\Model::query()->first();
            /**
             * Connection 直接查询
             */
            $lists = \Guanhui07\SwooleDatabase\Adapter\Manager::table('bd_live_plan')->first();
            var_dump(boolval($lists));
            /**
             * 协程销毁会自动归还连接(不需要手动处理)
             */
        });
    }
});
echo '所有任务用了:' . (microtime(true) - $s) . '秒';

class UserModel extends \Guanhui07\SwooleDatabase\Adapter\Model
{
    protected $table = 'user';

}

use Guanhui07\SwooleDatabase\Adapter\Manager as DB ;

$test = DB::table('user')->where('id', '>', 1)
    ->orderBy('id', 'desc')->limit(2)->get(['id']);
print_r($test->toArray());
$test = DB::select('select 1');
var_dump($test);