使用php的PDOStatement :: execute / fetch / rowCount测试数据库中是否存在值的问题?

时间:2012-09-26 01:32:13

标签: php mysql validation pdo

我的某个课程(我相信)存在问题。从高层次来说,我将一个表单发送到一个启动类的php文件。通过访问其中的几个方法,它确定值是否在数据库中。如果是,则返回布尔值。

以下是我认为是问题的代码:

public function territoryCheck($numberOut)

    {

        $this->numberOut = $numberOut;



        //Execute test

        $this->checkConnect();

        $stmt = $this->dbh->prepare("SELECT t_id FROM Territory WHERE t_id = :param1");

        $stmt->bindParam(':param1', $this->numberOut);

        $stmt->execute();   
        $count = $stmt->rowCount();



        //Determine value of test

        if($count == 0)

        {

            return FALSE;

        }   

    }



    public function publisherCheck($lName, $fName)

    {

        $this->lName = $lName;

        $this->fName = $fName;



        //Execute test

        $this->checkConnect();

        $stmt = $this->dbh->prepare("SELECT p_id FROM People WHERE lastName = :param1 AND firstName = :param2");

        $stmt->bindParam(':param1', $this->lName);

        $stmt->bindParam(':param2', $this->fName);

        $stmt->execute();
        $count = $stmt->rowCount();



        //Determine value of test

        if($count == FALSE)

        {

            return FALSE;

        }

        else

        {

            $dummyvar = $stmt->fetch();

            $this->p_id = implode($dummyvar);

        }



    }



    public function isTerritoryOut($numberOut)

    {

        //Execute test

        $this->checkConnect();

        $this->numberOut = $numberOut;

        $stmt = $this->dbh->prepare("SELECT t_id FROM checkIn WHERE t_id = :param1");

        $stmt->bindParam(':param1', $this->numberOut);

        $stmt->execute();
        $count = $stmt->rowCount();



        //Determine value of test

        if($count != 0)

        {

            return TRUE;

        }   

    }

有三种方法,每种方法都会返回true或false。 我正在使用execute(),fetch()和最后的rowCount()来测试我试图模拟我想要的值。似乎都没有用。这是调用这些方法的代码:

//Begin tests

    $checkOut->territoryCheck($numberOut);

    if($checkOut == FALSE)

    {

        $fail = "Territory number ".$numberOut." does not exist in our records. Please enter a valid territory. For more information, navigate to About.<\ br>";

    }



    $checkOut->publisherCheck($lName, $fName);

    if($checkOut == FALSE)

    {

        if($fail !== "")

            $fail .= "The publisher, ".$fName." ".$lName.", is not in our records. For more information, navigate to About.<\ br>";

        else

            $fail = "The publisher, ".$fName." ".$lName.", is not in our records. For more information, navigate to About.<\ br>";

    }



    $checkOut->isTerritoryOut($numberOut);

    if($checkOut === TRUE)

    {

        if($fail !== "")

            $fail .= "Territory number ".$numberOut." is currently checked out. Either the wrong number was entered or the territory hasn't been properly checked in.<\ br>";

        else

            $fail = "Territory number ".$numberOut." is currently checked out. Either the wrong number was entered or the territory hasn't been properly checked in.<\ br>";

    }

为清楚起见,在代码中较早时将失败设置为“”。会发生的是,当我故意创建应该失败的情况时,它会经历所有这些测试,就像它们已经过去一样。例如,我的数据库中只有区域编号1-130。如果我输入150,它基本上告诉我它存在。

我不确定发生了什么,类型转换? == vs. ===?等。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我必须承认,原因是一个相当明显的问题,很明显,我甚至没有考虑过它。在每个测试中,我只是调用方法而不指定任何变量来接受返回值(即$checkOut->publisherCheck();而不是$test = $checkOut->publisherCheck();。无论如何,它现在都有效。