Jo'vani7
Jo'vani7

Reputation: 53

Fatal error: Call to a member function rowCount() on a non-object

I'm using PDO in my login (as instructed previously over sqli), and I have tried the following, but yet I am getting this Fatal Error, and cannot figure out what to give it, so it satisfies the error:

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

Then I tried this:

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

Yet I still get this: Fatal error: Call to a member function rowCount() on a non-object

Here's is what I started with before the changes:

$count = $query->rowCount();

Lastly, here's a better snippet so you can get an idea of what's involved:

<?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!";
        }
    }           
?>

Can anyone point where I'm going wrong here. This is my only error this is being thrown.

Upvotes: 3

Views: 28053

Answers (2)

Whyle
Whyle

Reputation: 1

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

Try this.

Upvotes: -1

stwalkerster
stwalkerster

Reputation: 1808

You appear to be overwriting $query with the boolean return value from execute(), leaving you with a non-object value (boolean) which you're trying to call a method on.

Try something like this:

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();

    // ...
}

Upvotes: 6

Related Questions