Connect to a sqlite Database with PHP PDO

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

Tags

 PHP  PDO  sqlite  web development