PHP PDO SQLite prepared statement issues

帅比萌擦擦* 提交于 2019-12-07 09:05:49

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/4584788/php-pdo-sqlite-prepared-statement-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!