溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

larval中怎樣捕獲mysql錯誤

發布時間:2020-11-09 09:43:37 來源:億速云 閱讀:162 作者:小新 欄目:MySQL數據庫

這篇文章主要介紹了larval中怎樣捕獲mysql錯誤,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

larval捕獲mysql錯誤的方法:1、使用errorInfo變量返回SQLSTATE錯誤和消息;2、使用異常處理程序“app/Exceptions/Handler.php并偵聽QueryExceptions”將所有SQL錯誤記錄到數據。

Laravel使用PDO,因此您可以使用errorInfo變量返回SQLSTATE錯誤和消息?;旧?,您需要使用$e->errorInfo;

如果要將所有SQL錯誤記錄到數據庫中,可以使用異常處理程序(app/Exceptions/Handler.php并偵聽QueryExceptions。像這樣的:

public function render($request, Exception $e)
{
    switch ($e) {
        case ($e instanceof \Illuminate\Database\QueryException):
            LogTracker::saveSqlError($e);
            break;
        default:
            LogTracker::saveError($e, $e->getCode());
    }
    return parent::render($request, $e);
}

然后你可以用這樣的東西:

public function saveSqlError($exception)
{
    $sql = $exception->getSql();
    $bindings = $exception->getBindings()
    // Process the query's SQL and parameters and create the exact query
    foreach ($bindings as $i => $binding) {
        if ($binding instanceof \DateTime) {
            $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
        } else {
            if (is_string($binding)) {
                $bindings[$i] = "'$binding'";
            }
        }
    }
    $query = str_replace(array('%', '?'), array('%%', '%s'), $sql);
    $query = vsprintf($query, $bindings);
    // Here's the part you need
    $errorInfo = $exception->errorInfo;
    $data = [
        'sql'        => $query,
        'message'    => isset($errorInfo[2]) ? $errorInfo[2] : '',
        'sql_state'  => $errorInfo[0],
        'error_code' => $errorInfo[1]
    ];
    // Now store the error into database, if you want..
    // ....
}

感謝你能夠認真閱讀完這篇文章,希望小編分享larval中怎樣捕獲mysql錯誤內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女