問(wèn)題描述
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 無(wú)法連接到主機(jī),fatal error
會(huì)顯示用戶名和密碼.
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
中關(guān)閉 display_error=0
,但這樣我就無(wú)法知道當(dāng)我的主機(jī)沒有響應(yīng)時(shí).
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.
有沒有辦法修改錯(cuò)誤信息?
Is there a way I can modify the error message?
推薦答案
錯(cuò)誤處理和錯(cuò)誤報(bào)告之間存在差異.
There is a difference between error handling and error reporting.
- 錯(cuò)誤處理是防止您的最終用戶看到任何堆棧跟蹤、重要信息或自動(dòng)生成的錯(cuò)誤消息的過(guò)程.它還可以通過(guò)使用 try catch 塊來(lái)修改腳本的運(yùn)行方式.
- 錯(cuò)誤報(bào)告定義了給定腳本將報(bào)告哪些信息.
- 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.
為了正確處理錯(cuò)誤,我認(rèn)為 ini_set('display_errors',0);
是更好的方法.您不希望屏幕上顯示任何錯(cuò)誤消息.
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.
但是,我想獲得所有可能的錯(cuò)誤信息,所以我使用了error_reporting(E_ALL);
.
However, I want to have all possible information on errors, so I use error_reporting(E_ALL);
.
錯(cuò)誤寫在文件error_log 中,該文件通常與您的index.php(或任何直接調(diào)用的PHP 文件)位于同一級(jí)別.您也可以從您的 cPanel 訪問(wèn)它.
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.
您的錯(cuò)誤可能未被捕獲,因?yàn)槟拇a位于命名空間中,而您想要捕獲全局命名空間 PDOException
.使用 指示您正在尋找全局
PDOException
的腳本.一旦發(fā)現(xiàn)錯(cuò)誤,就可以使用 的常規(guī)方法回顯您想要的內(nèi)容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;
}
}
這會(huì)向您發(fā)送一封郵件,其中包含錯(cuò)誤的痕跡,防止您的用戶在告訴您出現(xiàn)問(wèn)題時(shí)看到它.
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 語(yǔ)句返回的錯(cuò)誤代碼.
Here is the reference for the error codes returned by PDO statements.
這篇關(guān)于未捕獲的 PDOException 顯示用戶名和密碼的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!