【问题标题】:Database connection and die issue数据库连接和死机问题
【发布时间】:2017-05-07 12:24:05
【问题描述】:
 if(DB::connection()->getDatabaseName()){
        $data = User::get(['id','first_name','last_name']);
        return View::make('index')->with('data',$data);
    }
    else{
        $request->session()->put('error','Could not connect to the database.  Please check your configuration.');
        return view::make('errors.503');  
    }   

当 DB 未连接时,我的 else 部分不工作。如何在 laravel 5.2 中处理所有数据库连接和死异常

【问题讨论】:

    标签: php mysql laravel die


    【解决方案1】:

    DB::connection()->getDatabaseName() 从您的配置中检查名称,这一定是为什么总是评估为 true 的原因

    试试DB::connection()->getPdo();,如果失败会抛出异常(使用try/catch)。

    编辑:

    try {
        DB::connection()->getPdo();
        $data = User::get(['id','first_name','last_name']);
        return View::make('index')->with('data',$data);
    } catch (\PDOException $e) {
        $request->session()->put('error','Could not connect to the database.  Please check your configuration.');
        return view::make('errors.503');  
    }
    

    【讨论】:

    • 我试试(try/catch)。但无法解决问题你能告诉我尝试/捕捉的正确方法吗? @vivoconunxino
    • This page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS 这个错误来了。
    猜你喜欢
    • 2020-10-15
    • 2020-04-06
    • 1970-01-01
    • 2017-09-15
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多