开发者

PHP PDO SQLite prepared statement issues

开发者 https://www.devze.com 2023-02-01 23:55 出处:网络
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,

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()->conn开发者_开发技巧ection->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.


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 );
  // . . .
?>


try declaring the $statement variable outside the first try block. e.g.

$statement = null;

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

精彩评论

暂无评论...
验证码 换一张
取 消