-2

I'm getting the error:

Call to a member function fetch() on a non-object

The line this refers to is:

$getProjectIdResult = $stmt->fetch();

Now, I think from this error that there must be something wrong with my database query, since the documentation says PDO query returns false on failure. I'm having trouble identifying what is causing the issue.

I've tried wrapping the fetch in a try/catch, with

$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

However the catch isn't triggered and I just get the original fatal error so I haven't been able to get a more specific error.

classes.php

class Query extends Connection {

    public function getProjectID($surveyID) {

        $query_getProjectID = "SELECT projectID FROM test WHERE surveyID = :surveyID";

        $query_getProjectID_params = array(

            ':surveyID' => $surveyID
        );

        try {

            $stmt = $this->db->prepare($query_getProjectID);
            $stmt = $stmt->execute($query_getProjectID_params);
        }

        catch (PDOException $ex) {

            die("Failed to get project ID: " . $ex->getMessage());
        }

        $getProjectIdResult = $stmt->fetch();

        $getProjectID = $getProjectIdResult['projectID'];       

        return $getProjectID;
    }
}

test.php

include_once("includes/classes.php");
include_once("includes/functions.php");

// Bind $_GET data
// localhost/panel/test.php?surveyID=3&status=1&respondentID=666
// Expected result: 111

$surveyID = sanitise($_GET['surveyID']);
$status = sanitise($_GET['status']);
$respondentID = sanitise($_GET['respondentID']);


$con = new Connection();
$query = new Query();

$query->getProjectID($surveyID);

$con->closeConnection();

I've ruled out the sanitise function causing an issue by testing with and without it.

I apologise as I know this is probably just another amateur making another amateur mistake judging by how many posts there are by the same title.

d.abyss
  • 204
  • 1
  • 4
  • 26
  • 1
    Possible duplicate of [My PDO Statement doesn't work](http://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work) – Masivuye Cokile Feb 17 '17 at 12:54
  • @MasivuyeCokile I would disagree, I'm already aware of what the error normally means - I've just messed up somewhere and having trouble spotting it with my limited knowledge – d.abyss Feb 17 '17 at 13:00

1 Answers1

1

When you call

$stmt = $stmt->execute($query_getProjectID_params);

You assign the return-value of execute() to $stmt, overwriting the variable, making it a boolean instead of an object. When you continue, $stmt no longer holds the PDOStatement object, but is now a boolean.

The solution is simply to remove the overwrite of your object, like this (remove $stmt = in front).

$stmt->execute($query_getProjectID_params);
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Thanks bro, that was exactly the problem :) I knew it would be something small – d.abyss Feb 17 '17 at 13:10
  • Glad this helped you solved your issue! Please accept and upvote if this was the solution :-) *Cheers!* – Qirel Feb 17 '17 at 13:14
  • Yeah I will, I was checking out the duplicate question and wondering why I was getting downvotes. I don't see how it is a duplicate in any way.. All he does is explain how to get errors showing, I mentioned in my post I already have that :( – d.abyss Feb 17 '17 at 13:20