尝试/在PHP中捕获不打印任何东西

时间:2014-11-24 16:22:27

标签: php

嘿,每个人都有一段时间了,我曾经使用过try / catch块,但我想再次开始使用它们只是出于错误处理和正确实践的目的。我的代码如下,

    $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)

更新

<?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

思想?

1 个答案:

答案 0 :(得分:2)

请阅读PDOException文档中的第一行:

  

表示PDO引发的错误。你不应该抛出PDOException   来自你自己的代码。

只需抛出并抓住常规旧Exception。这也会捕获从它继承的PDOException。

这也为您提供了一种更好的方法来区分PDO抛出的实际异常和您自己的异常。顺便说一句,看起来你有很多情况下,当PDO遇到错误并抛出异常时,你会冗余地抛出异常。只会抓住第一个异常,所以在很多情况下,你的抛出永远不会被执行。

另外,为什么还要在更新之前使用SELECT?你基本上只是在浪费一个查询,因为你没有对选定的信息做任何事情。也许只是为了更新和处理email_code不存在的情况而采取行动。