6

是否可以指定 MySQLi 向 PHP 默认的“error_log”指令发送任何错误和警告?我似乎找不到类规范的任何错误选项,并且我不希望像这样手动处理错误:

if ($result = $mysqli->query("...")) {  }
else
    handle $mysqli->error;
4

2 回答 2

4

好吧,一种方法是覆盖类:

class myMySQLi extends MySQLi {

    public function query($query, $resultmode = MYSQLI_STORE_RESULT) {
        $res = parent::query($query, $resultmode);
        if (!$res) {
            //handle error
        }
        return $res;
    }
}

然后像往常一样使用,除了不是通过创建连接new MySQLi(),使用new myMySQLi()。除了错误处理之外,它的运行方式相同。我经常这样做,在错误时抛出异常并向 MySQLi 添加额外的功能......

于 2010-05-28T20:14:53.733 回答
0

当然,这是可能的。您所要做的就是启用自动错误报告并告诉 PHP 将所有错误记录到文件中。

在使用 mysqli 打开连接之前,您应该确保启用了这两个设置:

ini_set("log_errors", 1); // this can be set in INI file too, together with error_log path
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$mysqli = new mysqli('localhost', /** ... */);

You don't need to check for mysqli errors manually anymore and all errors will be logged to a file.

于 2021-02-14T14:16:42.153 回答