ruesin/mysql

MySQL utils classes

v0.0.1 2019-02-16 08:01 UTC

This package is auto-updated.

Last update: 2022-01-29 03:09:25 UTC


README

基于Medoo的单例类,没有对Medoo做任何修改,仅为了方便使用而加了层单例壳。

  1. 使用setConfig($name, $config)加载配置到静态属性$configs中,$name为连接名,$config为连接选项参数
  2. 使用getInstance($name)获取指定连接名$name\Medoo\Medoo实例
  3. 获取的即为Medoo实例,直接使用即可
$configs = [
    'blog' => [
        'driver' => 'mysql',
        'host' => '127.0.0.1',
        'database' => 'blog',
        'username' => 'root',
        'password' => 'root',
        'port' => '3306',
        'retry_exception' => [
            // 'MySQL server has gone away'
        ]
    ],
    'web' => [
        'driver' => 'mysql',
        'host' => '127.0.0.1',
        'database' => 'web',
        'username' => 'root',
        'password' => 'root',
        'port' => '3306'
    ],
];
//加载配置到静态属性
foreach ($configs as $key => $config) {
    \Ruesin\Utils\MySQL::setConfig($key, $config);
}

//获取连接实例
$mysql = \Ruesin\Utils\MySQL::getInstance('blog');

//执行查询
$mysql->query("SELECT sleep(30);");
$mysql->query("SELECT sleep(10);");
\Ruesin\Utils\MySQL::getInstance('blog')->query("SELECT sleep(5);");

//关闭连接
\Ruesin\Utils\MySQL::close('blog');

//清除所有连接
\Ruesin\Utils\MySQL::clear();