Posted 2020-08-14 with tags PHP, PDO, sqlite, web development

Connect to a sqlite databsae in PHP by passing the full path to the database prefixed by sqlite: as the PDO connection string, as follows:

$pdo = new \PDO("sqlite:/var/www/google.com/database.sqlite");

Full Example with Query

try {
    $pdo = new \PDO("sqlite:/var/www/google.com/database.sqlite");
    $st = $pdo->query('select * from posts;');
    $posts = [];
    while ($row = $st->fetch(\PDO::FETCH_ASSOC)) {
        $posts[] = $row;
    }
} catch (\Exception $e) {                                                                                                                                           
    \Log::error(__METHOD__." failed.\tMessage: ".$e->getMessage());                                                                                                 
}