php单独使用laravel数据库 | laravel手动关闭数据库连接

17 篇文章 0 订阅

安装

composer require illuminate/database

laravel单独使用案例

增删改查-手动关闭数据库

<?php
date_default_timezone_set("PRC");
define("__UTILS__", __DIR__.DIRECTORY_SEPARATOR.'utils'.DIRECTORY_SEPARATOR);// 工具类、自定义函数存放目录
define("__CONFIG__", __DIR__.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR);// config配置文件存放目录
define("__RUNTIME__", getcwd().DIRECTORY_SEPARATOR.'runtime'.DIRECTORY_SEPARATOR);// 运行目录->log, cookie等
define("__RUNTIMELOG__", getcwd().DIRECTORY_SEPARATOR.'runtime'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR);// 运行目录->log, cookie等
require __CONFIG__.'config.php';
require __UTILS__.DIRECTORY_SEPARATOR.'common.php';






// 在这里引入Composer的自动加载文件
require 'vendor/autoload.php';



use Illuminate\Database\Capsule\Manager as Capsule;


class DB {
    private static $connection = [];
    private static $instance = null;
    private static $dbConfig = null;
    private static $dbName = 'default';
    protected function __construct() {

    }

    // 数据库驱动初始化
    private static function getInstance($dbName = null): Object
    {
        if (self::$dbConfig === null) {
            self::$dbConfig = require __CONFIG__.'database.php';
        }
        if ($dbName === null || empty(self::$dbConfig[$dbName])) {
            $dbName = self::$dbName;
        }
        if (self::$instance === null) {
            self::$instance = new Capsule;
            self::$instance->addConnection(self::$dbConfig[$dbName], self::$dbName);
            // 设置全局静态可访问DB
            self::$instance->setAsGlobal();
            // 启动Eloquent (如果只使用查询构造器,这个可以注释)
            self::$instance->bootEloquent();
        }



        if (self::$dbName != $dbName) {
            self::$dbName = $dbName;
            if (empty(self::$connection[$dbName])) {
                self::$instance->addConnection(self::$dbConfig[$dbName], self::$dbName);
                self::$connection[$dbName] = true;
            }
        }
        return self::$instance->connection($dbName);
    }

    // 关闭链接
    public static function close($dbName = null) {
        if ($dbName === null || empty(self::$dbConfig[$dbName])) {
            $dbName = self::$dbName;
        }
        self::$instance->connection($dbName)->disconnect();
        self::$connection[$dbName] = null;
        if (self::$dbName == $dbName) {
            self::$dbName = 'default';
        }
    }

    // 关闭所有链接
    public static function closeAll() {
        foreach (self::$connection as $dbName => $item) {
            self::$instance->connection($dbName)->disconnect();
        }
        self::$connection = [];
        self::$dbName = 'default';
    }

    // 获取链接句柄
    public function connection($dbName) {
        return self::getInstance($dbName);
    }

    // 表操作
    public function table(...$param) {
        return self::getInstance()->table(...$param);
//        return call_user_func_array(array(self::getInstance(), 'table'), func_num_args());
    }

    // 增
    public static function insert(...$param) {
        return self::getInstance()->insert(...$param);
    }

    // 删
    public static function delete(...$param) {
        return self::getInstance()->delete(...$param);
    }

    // 改
    public static function update(...$param) {
        return self::getInstance()->update(...$param);
    }

    // 查
    public static function select(...$param) {
        return self::getInstance()->select(...$param);
    }

    // 运行一个通用语句
    public static function statement(...$param) {
        return self::getInstance()->statement(...$param);
    }
}

// #######################
// 使用案例
// #######################
$res = [];
$connectRes = [];
$res[] = DB::table('table_1')->first(); // 默认连接 default

$connectRes[] = DB::select('SELECT connection_id( ) AS id');

$res[] = DB::connection('mysql_2')->table('table_2')->first(); // 连接2
$connectRes[] = DB::select('SELECT connection_id( ) AS id');

DB::close(); // 关闭默认连接
DB::close('mysql_2');// 关闭mysql_2
$connectRes[] = DB::select('SELECT connection_id( ) AS id');
print_r($connectRes);

参考文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值