skip to Main Content

I’m using nginx in docker from trafex/php-nginx
But I don’t get SQLite working.
The following php code

<?php
$db = new SQLite3('/var/private/anmeldungen.db');
$db->close();

throws this error [error] 9#9: *33 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Class "SQLite3" not found in /var/www/html/phpinfo.php:3

I’m using a dockerfile to install the sqlite package:

FROM trafex/php-nginx:latest

USER root
RUN apk add php8-pdo_sqlite

USER nobody

And in the PHPInfo I can see that the package is loaded:
phpinfo part 1
phpinfo part 2

Does anyone have an idea?

2

Answers


  1. SQLite is included with PHP, you have to uncomment it in the php.ini file. But first you have to uninstall the one you installed because otherwise PHP will complain that there are two. I had the same problem, albeit not inside a Docker container. PHP agreed that SQLite was installed but the CLI wouldn’t use it.

    Login or Signup to reply.
  2. Your package is wrong, PDO is PHP Data Object which is a higher layer designed to be able to work with any lower database protocol such as SQLite, MySQL, Postgres…, so implementing via PDO may help you a lot in long run if you would like to switch to another database without changing much (there may be some incompatible features but not much) of your source code.

    To init a SQLite via PDO you have to do so:

    $dbh = new PDO('sqlite:/tmp/foo.db');
    

    You can read more about that here https://www.php.net/manual/en/ref.pdo-sqlite.php.

    If you want to stick with native SQLite (to use your SQLite3 class), you will have to install package php8-sqlite3 but not the php8-pdo_sqlite3

    apk add php8-sqlite3
    

    This will work with your current code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search