致命错误:在非对象上调用成员函数rowCount()

时间:2014-11-25 02:29:16

标签: php pdo

我在我的登录中使用PDO(之前通过sqli指示),我尝试了以下内容,但是我得到了这个致命错误,并且无法弄清楚要给它什么,所以它满足了错误:

if($query->rowCount() > 0) 
{
   // session stuff
   // refresh page
}

然后我尝试了这个:

if($query->rowCount() == 1) 
{
   // session stuff
   // refresh page
}

然而我仍然得到这个:致命错误:在非对象上调用成员函数rowCount()

以下是我在更改之前开始的内容:

$count = $query->rowCount();

最后,这是一个更好的代码段,以便您了解所涉及的内容:

<?php
include("/scripts/Connections.php");

$email = $_POST['email'];
$username = $_POST['username'];
$password = md5($_POST['password'], "DDerehOjhdfDDf$$##%^)-=_/.#$#dkfsj!`~efjkf(*)/)sD");
$confPassword = md5($_POST['conPassword'], "DDerehOjhdfDDf$$##%^)-=_/.#$#dkfsj!`~efjkf(*)/)sD");

if(isset($email, $username, $password, $confPassword)) {
    if(strstr($email, "@")) {
        if($password == $confPassword) {
            $query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");
            $query = $query->execute(array(
            $username,
            $email
        ));

        $count = $query->rowCount();

        if($count == 0) {
            $query = $dbc->prepare("INSERT INTO memebers SET username = ?, email = ?, password = ?");
            $query = $query->execute(array(
                $username,
                $email,
                $password
            )); 
            if($query) {
                echo "Your account has been registered, you may login!";                
        }           
        }
        else {
            echo "A user already exists with that username/password.";
        }
    }
    else {
            echo "Your passwords do not match!";
        }
}
else {
            echo "Invalid email address!";
        }
    }           
?>

任何人都可以指出我在这里出错的地方。这是我抛出的唯一错误。

2 个答案:

答案 0 :(得分:6)

您似乎使用$query的布尔返回值覆盖execute(),留下您尝试调用方法的非对象值(布尔值)。< / p>

尝试这样的事情:

if($password == $confPassword) {
    $query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");

    $result = $query->execute(array(
        $username,
        $email
    ));

    // check the value of $result is true here - if not,
    // your query has failed to execute and handle the error
    // appropriately.

    $count = $query->rowCount();

    // ...
}

答案 1 :(得分:0)

        $query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");
        $query->execute(array($username, $email)):;
        $count = $query->rowCount();
        echo "Value is " . $count;
  

试试这个。