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.