2011-12-01 94 views
0

我在我的類方法中使用try/catch塊,如果得到一個異常,我記錄錯誤。但是我想告訴「用戶」一個數據庫查詢/ etc失敗 - 這個問題應該儘快解決。 我可以在我的方法中使用異常中的die(),但這不會幹,因爲我必須重新輸入它很多,所以關於如何執行此操作的任何建議。全局php PDO處理錯誤

實施例方法:

public function login($username, $password) { 
    try { 
     $this->STH = $this->DBH->prepare("SELECT id, baned, activated FROM users WHERE username = ? AND password = ?"); 
     $this->STH->setFetchMode(PDO::FETCH_OBJ); 
     $this->STH->execute(array($username, $password)); 

     if (($row = $this->STH->fetch()) !== false) 
      return $row; 
    } catch (PDOException $e) { 
     //Log $e->getMessage(); 
     die('A database error occoured, we are working on the problem, and it should work in a few...'); 
    } 
} 

回答

1

這是確定重複自己在這種情況下,因爲作爲芯片的每個實例()傳遞一個唯一的消息。

3

如果你需要速戰速決,你可以設置一個全球性的異常處理程序,如:

function pdo_exception_handler($exception) { 
    if ($exception instanceof PDOException) { 
     // do something specific for PDO exceptions 
    } else { 
     // since the normal exception handler won't be called anymore, you 
     // should handle normal exceptions yourself too 
    } 
} 
set_exception_handler('pdo_exception_handler'); 
+0

我無法得到它的工作,通過上述的方法,沒有結果輸出,我做了一個錯誤故意...... – John

+0

該處理程序將僅被調用爲未捕獲的異常,因此如果您已經在代碼的某處捕獲到異常,則應該再次拋出異常,以便全局處理程序可以捕獲它。 – rabusmar

+0

我該怎麼做 – John