2014-11-24 162 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