0

I have a php project using SQLite as the database. I have a class called SQLiteConnection

namespace App;

/**
 * SQLite connection
 */
class SQLiteConnection {
    /**
     * PDO instance
     * @var type 
     */
    private $pdo;

    /**
     * return in instance of the PDO object that connects to the SQLite database
     * @return \PDO
     */
    public function connect() {
        if ($this->pdo == null) {
            $this->pdo = new \PDO("sqlite:" . Config::PATH_TO_SQLITE_FILE);
        }
        return $this->pdo;
    }
}

when I make a call to $stmt->fetch() in the code below, I get this error: Fatal error: Uncaught Error: Call to a member function fetch() on boolean in...

use App\SQLiteConnection;
$pdo = (new SQLiteConnection())->connect();
if (!$pdo)
{
  die("Execute query error, because: ". print_r($this->pdo->errorInfo(),true) );
}
else
{
$stmt = $pdo->query('select id, fullname from employees order by fullname');
$employees = [];
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
    $employees[] = [
        'id' => $row['id'],
        'fullname' => $row['fullname']
    ];
}

I have not worked with pdo much and so I am not sure what to look for. I have read some other posts about the pdo object being boolean because a connection was not made or a table was missing. My query table is there and I am not sure about the connection, but my code to catch a false pdo value does not run.

Thank you.

Ryan
  • 650
  • 3
  • 15
  • 49
  • Your query failed. Check for errors to find out why. You can also try running the query directly in the database (CLI, PHPMyAdmin, MySQL Workbench) to see if it works there. – aynber Aug 22 '19 at 20:10
  • Read the error message. "Call to a member function fetch() on boolean" So, what are you trying to run `fetch()` on? Must mean that `$stmt` is a boolean. – miken32 Aug 22 '19 at 20:11

0 Answers0