İbrahim Duran
İbrahim Duran

Reputation: 278

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

I know that error occurs usually when query returned false but this time this occurs with no reason! (or just I'm making a big mistake)

    if(!$security_SenderId){
        $getbaseticketqry = $this->db->prepare("SELECT * FROM `tickets` WHERE `ticket_safeid` = '?'");
        $getbaseticket = $getbaseticketqry->execute(array($ticket_safeid));
    }else{
        $getbaseticketqry = $this->db->prepare("SELECT * FROM `tickets` WHERE `ticket_safeid` = '?' AND `ticket_sender` = '?'");
        $getbaseticket = $getbaseticketqry->execute(array($ticket_safeid, $security_SenderId));
    }

    if($getbaseticket === false){
        return false;
    }else{
        $baseticket = $getbaseticket->fetch(PDO::FETCH_ASSOC);
    }

I've theese lines in a function that returns support ticket information as array but as I said the error occurs when I tried to fetch the ticket information. I tried to check mysql errors just before fetch line by enabling the pdo debug mode and db->errorInfo() but it didn't work.

What can the problem be here?

Edit:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);

class TICKET_MANAGER
{
    function __construct($dbhost, $dbname, $dbuser, $dbpass) {
        try{
            $this->db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuser, $dbpass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            die('Connection failed: ' . $e->getMessage() );
        }
    }

    function viewTicket($ticket_safeid, $security_SenderId = false)
    {
        try{
            if(!$security_SenderId){
                $getbaseticketqry = $this->db->prepare("SELECT * FROM `tickets` WHERE `ticket_safeid` = ?");
                $getbaseticket = $getbaseticketqry->execute(array($ticket_safeid));
            }else{
                $getbaseticketqry = $this->db->prepare("SELECT * FROM `tickets` WHERE `ticket_safeid` = ? AND `ticket_sender` = ?");
                $getbaseticket = $getbaseticketqry->execute(array($ticket_safeid, $security_SenderId));
            }
        }catch(PDOException $e){
            die('Mysql error: ' . $e->getMessage() );
        }
        ...

    }

    ...
}

Upvotes: 0

Views: 566

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

It's the quotes around all your '?' - Remove them.

Read the manual

from the manual:

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");

Exceptions should have told you that error

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)

and used right after you've connected to your DB.


-http://php.net/manual/en/pdo.error-handling.php

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Query:

try {

// your query

}
    catch(PDOException $e){
     print  $e->getMessage();
    }

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Upvotes: 2

Related Questions