問題描述
try {
self::$dbinstance = new PDO(
"mysql:host=$c[host];dbname=$c[dbname]", $c['user'], $c['password']
);
self::$dbinstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Errors" . $e->getMessage();
}
在上面的代碼中,如果 PDO 無法連接到主機,fatal error
會顯示用戶名和密碼.
In the above code, if PDO fails to connect to the host, a fatal error
reveals the username and password.
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003]
Can't connect to MySQL server on '172.25.102.65' (10060)' in
D:xampphtdocsmytestwh_client_2.1classesimportmodule-class.php:33 Stack trace: #0
D:xampphtdocsmytestwh_client_2.1classesimportmodule-class.php(33): PDO-
>__construct('mysql:host=172....', 'host', 'password') #1
一種可能的方法是在 php.ini
中關閉 display_error=0
,但這樣我就無法知道當我的主機沒有響應時.
One possible way is to turn the display_error=0
off in php.ini
, but this way I won't able to know that when my host is not responding.
有沒有辦法修改錯誤信息?
Is there a way I can modify the error message?
推薦答案
錯誤處理和錯誤報告之間存在差異.
There is a difference between error handling and error reporting.
- 錯誤處理是防止您的最終用戶看到任何堆棧跟蹤、重要信息或自動生成的錯誤消息的過程.它還可以通過使用 try catch 塊來修改腳本的運行方式.
- 錯誤報告定義了給定腳本將報告哪些信息.
- Error handling is the process of preventing your end users to see any stack trace, vital information or automatically generated error messages. It can also modify the way your script runs by using a try catch block.
- Error reporting defines which information will be reported by a given script.
為了正確處理錯誤,我認為 ini_set('display_errors',0);
是更好的方法.您不希望屏幕上顯示任何錯誤消息.
To handle errors properly, I think that ini_set('display_errors',0);
is the better approach. You do not want any error message displaying on the screen.
但是,我想獲得所有可能的錯誤信息,所以我使用了error_reporting(E_ALL);
.
However, I want to have all possible information on errors, so I use error_reporting(E_ALL);
.
錯誤寫在文件error_log 中,該文件通常與您的index.php(或任何直接調用的PHP 文件)位于同一級別.您也可以從您的 cPanel 訪問它.
Errors are written in a file, error_log, which usually resides at the same level as your index.php (or any PHP file called directly). You can also access it from your cPanel.
您的錯誤可能未被捕獲,因為您的代碼位于命名空間中,而您想要捕獲全局命名空間 PDOException
.使用 指示您正在尋找全局
PDOException
的腳本.一旦發現錯誤,就可以使用 的常規方法回顯您想要的內容PDOException 類.
Your error is probably uncaught because your code is in a namespace, whereas you want to catch the global namespace PDOException
. Use a to indicate your script you're looking for the global
PDOException
. Once you catch your error, you can echo the content you want, using the normal methods of the PDOException class.
try {
$db = new PDO (/*connection infos*/);
}
catch (PDOException $e) {
switch ($e->errorCode()) {
case 'HY000':
// Or whatever error you are looking for
// here it's the general error code
mail('your@email.com','connection problem',$e->getTraceAsString());
$db = new PDO (/*rollback connection infos of a local database*/);
break;
}
}
這會向您發送一封郵件,其中包含錯誤的痕跡,防止您的用戶在告訴您出現問題時看到它.
That would send you a mail, containing the trace of the error, preventing your user from seeing it while telling you something is wrong.
這里是參考 用于 PDO 語句返回的錯誤代碼.
Here is the reference for the error codes returned by PDO statements.
這篇關于未捕獲的 PDOException 顯示用戶名和密碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!