Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO SQLite prepared statement issues

I am trying to migrate a PHP app from MySQL to SQLite, and some things that used to work, simply stopped working now. I am using PDO through a custom database wrapper class (the class is a singleton, seems logical to do it like that).

The problem: When trying to execute a query on a prepared statement, it throws a "fatal error: Call to a member function execute() on a non-object ...".

Relevant code (narrowed it down to this, after some hours of var_dumps and try-catch):

Connection string:

$this->connection = new PDO("sqlite:"._ROOT."/Storage/_sqlite/satori.sdb");

Obviously, the $connection variable here is a private variable from the class.

The error happens here (inside a function that is supposed to perform database insert):

    try{
        $statement = self::getInstance()->connection->prepare($sql);
    }catch (PDOException $e){
        print $e->getMessage;
    }

    try{
        var_dump($statement);
        $statement->execute($input);
    }catch (Exception $e){
        print $e->getMessage();
    }

More accurately, it happens when I try to $statement->execute($input).

Any help appreciated. Thanks.

like image 880
dbozhinovski Avatar asked Dec 17 '22 18:12

dbozhinovski


1 Answers

You are probably getting a mySQL error when the statement is being prepared, but you don't have PDO configured to throw an exception in case of an error.

<?php

  $dbh = new PDO( /* your connection string */ );
  $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  // . . .
?>
like image 90
Pekka Avatar answered Dec 19 '22 08:12

Pekka