PHP PDO SQLite prepared problem reports

I am trying to port a PHP application from MySQL to SQLite, and some things that worked before just stopped working. I use PDO through my own database shell class (the class is singleton, it seems logical to do it like this).

Problem: When trying to execute a request in a prepared statement, it generates a "fatal error: call of the execute () member function for a non-object ...".

Relevant code (tapering to this after a few 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 is here (inside the function that should perform the 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 precisely, this happens when I try to execute $ statement-> execute ($ input).

Any help appreciated. Thanks.

+4
source share
2 answers

You probably get a mySQL error when preparing the statement, but you do not have a 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 ); // . . . ?> 
+10
source

try declaring the variable $ statement outside the first try block. eg.

 $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(); } 
+2
source

Source: https://habr.com/ru/post/1334067/


All Articles