简体   繁体   English

如何从MySQL获取最后一条错误消息?

[英]How to get last error message from MySQL?

I'm using debug_backtrace() and some basic styling to log my website errors. 我正在使用debug_backtrace()和一些基本样式来记录我的网站错误。

if (@mysql_error($this->conn))
   $error = 'mysql_error() response: ' . @mysql_error($this->conn);
else
   $error = 'non mysql error\r\n';

Sometimes it doesn't work. 有时它不起作用。 My log files are full of "non mysql error" but the errors always appear in functions which has database connections. 我的日志文件中充满了“非mysql错误”,但这些错误始终出现在具有数据库连接的函数中。 Like, getAllNews() function fails, but it logs it as non mysql error. 像, getAllNews()函数失败,但是将其记录为非mysql错误。

How can I log errors of my website and mysql correctly? 如何正确记录我的网站和mysql的错误?

I think you need to log error immediately after "error-able" mysql functions. 我认为您需要在“易出错”的mysql函数后立即记录错误。 And AFAIK, all error-able functions return FALSE on error, so you can check as below; 而AFAIK,所有可出错的函数均会在出错时返回FALSE ,因此您可以进行以下检查:

<?php
public function errorLog($error) {
    // concat error with mysql error
    $error .= $this->conn
        ? mysql_error($this->conn)
        : mysql_error();
    // log error
    error_log($error);
    // keep last error
    $this->_lastError = $error;
}

...

// remember suppressing error throws (with "@" sign)
$this->conn =@ mysql_connect(...);
if (false === $this->conn) {
    $this->errorLog("Connection failed: ");
}
//or
$query =@ mysql_query(...);
if (false === $query) {
    $this->errorLog("Query failed: ");
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM