2

以下是我可能的解决方案:

1)exit()用于终止脚本。(问题:影响整个页面内容的加载,而不仅仅是来自数据库的数据)

2)在页面中嵌入一个<div>元素,专门用于显示可能的异常。

标准方法是什么?

4

6 回答 6

3

如何处理数据库中的异常并没有真正的“标准”方法。这完全取决于具体的用例。例如,如果在第一种情况下无法建立数据库连接,则可能适合重定向到错误页面并立即向站点管理员发送电子邮件通知。另一方面,如果您无法执行单次插入(无论出于何种原因),则通知用户他们正在尝试的操作无法完成并要求他们再试一次可能更合适。

异常本质上是一种让(而不是您正在使用的编程语言或库)决定在出现问题时如何处理的方式。几乎总是,您会想要记录错误,然后由您决定最合适的响应。

也就是说,处理异常的适当方式是在您的应用程序中最有意义的方式。

于 2012-10-31T14:42:37.730 回答
1

选项 1) 不整洁,因为它提供了糟糕的用户体验

选项 2) 是首选。

使用 PDO,以下代码将显示异常并将其与时间戳一起存储。

try {  
    $dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);  
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    $stmt = $dbh->prepare("INSERT INTO table ( column ) values ( value )");  
    $stmt->execute(); 
    }  
catch(PDOException $e) {  
    echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]'). $e->getMessage()."\r\n", FILE_APPEND);  
}  

在测试后的生产站点上,应将 echo 语句修改为

echo "I'm sorry I'm afraid you can't do that.";
于 2012-10-31T21:59:43.970 回答
1

I was faced with the same problem in my project. First, I thought of a solution similar to that of david strachan, but eventualy I realized that this solution was not complete. I still need to have the debug information, but those should not be displayed to the user.

I came up with a solution that displayes to the user a message stating "System error, please try again, if the error persists, then please contact support". And also, at the same time, the error message is logged to an error-log file.

Here are the details:

function logException ($exception) {

    // Construct the error message to add to the log file
    $logDate = date("l, F j, Y  H:i:s");            
    $logMessage = "Date: " . $logDate . "\n";
    $logMessage = $logMessage . "Error Message: ". $exception->getMessage() . "\n";
    $logMessage = $logMessage . "Error Code: ". $exception->getCode() . "\n";
    $logMessage = $logMessage . "Error File: ". $exception->getFile() . "\n";
    $logMessage = $logMessage . "Error Line: ". $exception->getLine() . "\n";
    $logMessage = $logMessage . "Error Trace:\n". $exception->getTraceAsString() . "\n";
    $logMessage = $logMessage . "\n\n**************************************" . "\n\n\n";

    // Construct a log file name similar to the name of the script file that cause the error
    $info = pathinfo($exception->getFile());
    $extension = "." . $info["extension"];
    $logFile =  "../logs/" .  basename($exception->getFile(), $extension) . ".log";

    // Add the error message to the log file            
    error_log($logMessage, 3, $logFile);

    // Message to return to the user
    $errorMessage = "System error, please try again, if the error persists, then please contact support.";

    return $errorMessage;
} 


try {  
    $dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);  
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    $stmt = $dbh->prepare("INSERT INTO table ( column ) values ( value )");  
    $stmt->execute(); 
}  

// Catching all sorts of errors, including PDO errors
catch (Exception $e) {
    $message = logException ($e);
    $data['message'] = $message;
    $data['isSuccessful'] = false;
}

Please note the following:

  1. The error message is saved to a log file
  2. The log file is saved to a logs folder. The name of the log file is derived from the name of the file that generated the error. For example, if the file that caused the error is called "script.php", then the log file will be called "script.log" and will be saved inside the logs folder.
  3. The catch statement is catching all sorts of exceptions, and is not only restricted to PDO errors.

Now, you have a problem, what if you would like to throw an exception that would like the user to see (for example, you would like to throw an exception such as "This is wrong, try agian")? Well, I did come up with a solution that is extended over this one, but it is out of your questoin. But if you still would like to know, then please let me know.

Cheers.

于 2012-11-03T04:47:55.913 回答
1

您应该在日志文件中记录您的异常,并使用它来分析出了什么问题。但是从用户的角度来看,您应该向他们展示友好的陈述,例如“抱歉您的请求无法处理”。

于 2012-10-31T14:43:15.737 回答
0

try - catch 是我在 php 中处理异常的标准方式......如下所示:PHP EXCEPTIONS

try{
    some code here
}catch(Exception $e){
    failed response here
}

编辑详细说明:这只是捕获异常的一种方法。当一个人被抓住时该怎么办是留给开发商的价值判断。如果这是问题所在,我很抱歉。

于 2012-10-31T14:43:11.727 回答
0

您必须找到自己的方法来捕获错误。我正在使用

try { QUERY } catch(Exception $e) { Log and display error }
于 2012-10-31T14:44:06.097 回答