larval怎样捕捉mysql差错
发布时间:12/01 来源:未知 浏览:
关键词:
larval捕捉mysql差错的办法:1、运用errorInfo变量返回SQLSTATE差错和新闻;2、运用异样处置程序“app/Exceptions/Handler.php并侦听QueryExceptions”将所有SQL差错记载到数据。
举荐:《mysql视频教程》
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差错的细致内容,更多请关注 百分百源码网 其它相干文章!