views:

25

answers:

2

Somehow my execute statement says the object has no member "execute". What is wrong?

class EdlSqliteDb
{
const SQLITE_DRIVER = "sqlite:";

var $dbh;
var $qIndex = Array();

//
function EdlSqliteDb($dsn) 
{
    try 
    {
        $this->dbh = new PDO(self::SQLITE_DRIVER . $dsn);
    } 
    catch (PDOException $e)
    {
        echo "Error connecting: " . $e->getMessage() . ' ' . self::SQLITE_DRIVER . $dsn;
        die();
    }

    return;
}

//
function addQ($index,$q)
{
    $this->qIndex[$index] = $q;
}

//
function PrepareQ($index)
{
    try
    {
        $stmt = $this->dbh->prepare($this->qIndex[$index]);
    }
    catch (PDOException $e)
    {
        echo "Db Prepare Error: " . $e->getMessage();
        die();
    }
    return $stmt;
}

//
function DbExecutePrepared($index, $arrParameters)
{
    $stmt = $this->PrepareQ($index);
    if ($stmt->execute($arrParameters)) 
    {
        $row = $stmt->fetch();
        return $row;
    }
    else 
    {
        print "<p>dbquery(): database table update execute error</p>\n";
        die();
    }
}

}

+1  A: 

Add:

var_dump($stmt);

After:

$stmt = $this->PrepareQ($index);

Probably there's something wrong with it so $this->PrepareQ($index); is returning null, or an object that has no execute method, var_dumping the variable will help knowing what that variable is and debugging this issue.

aularon
+2  A: 

This is most likely due to a failed preparation.

Depending on error handling, PDO, instead of raising an exception, may just return false .

If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

Not entirely sure where this behaviour is defined, to be honest....

Update: here it is. The default behaviour is to fail silently. You need to set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION for exceptions to be risen at all times.

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
Pekka