How do I connect to an SQLite database using PDO?

How do I connect to an SQLite database using PDO?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The sqlite_open() Warning: A Legacy Trap

The error message you are encountering, file is encrypted or is not a database, is not actually an indication of file corruption or encryption. It is a specific symptom of using deprecated PHP functions that have been removed from modern versions of the language. The function sqlite_open() belongs to the old SQLite extension (php_sqlite.dll), which has been obsolete since PHP 5.3 and completely removed in PHP 7.

If your project requires connecting to an SQLite database today, you must use PDO (PHP Data Objects) or the newer SQLite3 extension. Since you specifically asked about PDO, here is how to migrate away from the deprecated API without breaking your logic.

The primary issue preventing your connection is likely that sqlite_open() expects a relative path if called from within the web root, but the error message often confuses users into thinking their file permissions or encryption are at fault. In reality, the function itself is simply gone in newer PHP environments (e.g., PHP 8.x or even many 7.x builds). When you attempt to call it, PHP throws a fatal error stating "Call to undefined function sqlite_open()". However, if you are seeing the specific warning about encryption, it implies you might be running an older PHP version where the function exists but has been strictly deprecated to warn you against its use.

To connect properly using PDO, you need to initialize a new PDO instance with the correct DSN (Data Source Name). SQLite supports the sqlite:// prefix directly in the DSN, which tells the driver to treat the string as a file path relative to the current working directory.

Here is the modern equivalent of your failing code:

<?php

$host = 'sqlite:///path/to/your/database.sqlite';
$username = '';
$password = '';
$charset = 'utf8mb4'; // Optional, ensure your DB supports this if using newer PHP versions

try {
    $pdo = new PDO($host, $username, $password);
    
    // Always set the error mode to exceptions for easier debugging
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Verify the connection by running a simple query
    $stmt = $pdo->query("SELECT sqlite_version()");
    $version = $stmt->fetchColumn();
    
    if (!$version) {
        throw new Exception("Could not connect to the database.");
    }
    
    echo "Connected successfully. SQLite Version: " . $version;
    
} catch (PDOException $e) {
    // Log the error appropriately in production
    error_log($e->getMessage());
    die("Connection failed: " . $e->getMessage());
}

Notice the shift from sqlite_open() to new PDO(). The path provided in the DSN (sqlite:///path/to/your/database.sqlite) is relative to the directory where your PHP script is executing. If your script resides in C:\xampp\htdocs\deepthi\combadd\, and your database file is located there as well, you must include three slashes after sqlite:. The first two indicate the protocol (sqlite://), and the third acts as the root of the filesystem for that protocol.

A common pitfall when switching to PDO with SQLite is forgetting to set the error mode to exceptions. By default, PDO returns warnings instead of throwing them, which often leads to silent failures where your script continues running without data being fetched or inserted. Setting PDO::ATTR_ERRMODE ensures you get a clear exception stack trace if the connection fails.

If you are using Laravel, the database abstraction layer handles this configuration automatically via the .env file and config/database.php. You would define your SQLite connection like this:

DB_CONNECTION=sqlite
DB_DATABASE=path/to/your/database.sqlite

This approach is robust because Laravel handles the DSN formatting and driver registration for you. For raw PHP scripts, ensure that the PDO SQLite extension is enabled in your php.ini file (often commented out as ;extension=pdo_sqlite). Without this extension loaded, even with correct code, you will receive a "Class 'PDO' not found" or similar fatal error.

Finally, remember that SQLite files are stored on disk and can be accessed by any process with read/write permissions to the file path. Ensure that your web server user (e.g., www-data or IIS AppPool) has write permissions if you intend to execute write operations like INSERT, UPDATE, or CREATE TABLE.

Our network