2014-11-24 161 views
0

嘿每個人都已經一段時間wince我已經嘗試/ catch塊,但我想開始再次使用它們只是爲了錯誤處理和正確的做法。我的代碼如下,嘗試/抓住在PHP不打印任何東西

$email_code = $_REQUEST['code']; //retrive the code from the user clicked link in the email 
    //database information 
    $dsn = 'mysql:host=localhost;dbname=primarydb'; 
    $username = 'root'; 
    $password = ''; 
    try { 

     //option for PDO allows for prepared SQL statements that will mazimize the prevention of sql injections and malicious attacks on the server and databases 
     $conn = new PDO($dsn, $username, $password); //establish the connection 
     $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //disable the php parse from parsing the statements. 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //allow error mode to be active in order to display any errors which may open up holes to attacks 
     //if the connection fails the try/catch block will pick it up 
     if (!$conn) { 
      throw new PDOException('Fatal error on connection'); 
     } else { 
      //prepare and exexcute the query to match the codes up 
      $stmt = $conn->prepare("SELECT email_code, active from primarydb.user WHERE email_code = ?"); 
      $stmt->bindParam(1, $email_code, PDO::PARAM_STR, 32); 
      //check to make sure that the statment executes properly 
      if (!$stmt->execute()){ 
       throw new PDOException("PDO ERROR ON EXECUTION:\n" . $stmt->errorInfo()); 
      } else { //statement has not failed 
       //get the row count 
       $count = $stmt->rowCount(); 
       //traverse the results 
       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
        //there can only be one! 
        if ($count != 1 || $row['active'] != 0) { 
         //generate error message 
         throw new PDOException("Wrong Code");  
        } else { 
         echo "working"; 
         //prepare the update statement 
         $stmt = $conn->prepare("UPDATE primarydb.user SET active = ? WHERE email_code = ?"); 
         $stmt->brindParam(1, 1, PDO::PARAM_INT); 
         $stmt->bindParam(2, $email_code, PDO::PARAM_STR, 32); 
         if (!$stmt->execute()) { 
          throw new PDOException("We're sorry but we can not update your profile at this time, plesae try again later. If this problem persists please contact customer service."); 
         } else { 
          print "Your account has now been activated and it is ready to use!"; 
         } 
        } 
       } 
      } 
     } 
    } catch(PDOException $e){ 
     //display error message if the database has failed in some manner 
     echo $e->getMessage(); 
    } 

我想知道爲什麼我沒有收到任何錯誤信息,然後如何解決這個問題,使我能夠避免將來再犯同樣的問題。如果有什麼遺漏或需要更多信息,請告訴我。否則,我認爲這非常簡單。

附加信息:我的推杆是說在的if/else,它最終停止顯示出來的一個是,當我檢查每塊工作的消息if($count != 1 || $row['active'] != 0)

UPDATE

<?php 
    $email_code = $_REQUEST['code']; //retrive the code from the user clicked link in the email 
    //database information 
    $dsn = 'mysql:host=localhost;dbname=primarydb'; 
    $username = 'root'; 
    $password = ''; 
    try{ 
     //option for PDO allows for prepared SQL statements that will mazimize the prevention of sql injections and malicious attacks on the server and databases 
     $conn = new PDO($dsn, $username, $password); //establish the connection 
     $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //disable the php parse from parsing the statements. 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //allow error mode to be active in order to display any errors which may open up holes to attacks 
     //prepare the update statement 
     $stmt = $conn->prepare("UPDATE primarydb.user SET active = ? WHERE email_code = ?"); 
     $stmt->bindParam('is', $a = 1, $email_code); 
     if($stmt->execute()){   
      print "Your account has now been activated and it is ready to use!"; 
     } 
    } catch(PDOException $e){ 
     //display error message if the database has failed in some manner 
     echo $e->getMessage(); 
    } 
?> 

產生了新的代碼,我不想脫離主題,但我想要一個完整的解決方案來解決這個問題。現在我收到以下錯誤

Strict Standards: Only variables should be passed by reference in C:\inetpub\wwwroot\mjsite\login\complete_registration.php on line 14 SQLSTATE[HY000]: General error: 2031

的思考?

+0

您是否打開了[錯誤報告](http://php.net/manual/en/function.error-reporting.php)? – 2014-11-24 16:24:28

+0

我這樣做,沒有任何顯示,我沒有收到任何解析錯誤或任何東西 – 2014-11-24 16:25:11

回答

2

請閱讀從PDOException文檔此第一行:

表示由PDO引發的錯誤。您不應該從您自己的代碼中拋出PDOException 。

只是拋出並抓住正常的舊Exception s。這也會捕獲從它繼承的PDOException。

這也給你一個更好的方法來區分PDO引發的實際異常和你自己的異常。順便說一下,看起來有很多情況下,當PDO遇到錯誤並拋出異常時,您會冗餘地拋出異常。只有第一個異常會被捕獲,所以在許多情況下,你的投擲永遠不會被執行。

此外,爲什麼還要在更新之前根本打擾SELECT?你基本上只是在浪費一個查詢,因爲你沒有對選定的信息做任何事情。也許只是爲了更新和處理email_code不存在的情況而行。

+0

你的答案對我有幫助,但除了select語句的冗餘之外,我沒有看到你在與其他的東西。 – 2014-11-24 16:37:52

+0

@MarkHill底線是你不應該拋出'PDOException',並且,因爲你在異常模式下運行PDO。您正在檢查並拋出異常的錯誤將會在PDO自己拋出異常之前有機會到達您的代碼之前引發異常。 – 2014-11-24 16:40:16

+0

哦,好吧,只要所有事情都在try外殼內完成,我的$ stmt變量拋出的任何錯誤都會被捕獲,無論如何? – 2014-11-24 16:41:41