1

I am working on a website which needs to interact with an SQLite database. This is my first project using PHP and I'm running into some issues. So far I have:

config.php:

...
$connection = new SQLiteConnection();
$connection->connect();

And in SQLiteConnection, $connection->connect() does pretty much this:

try {
    $rootDir = $_SERVER["DOCUMENT_ROOT"];
    $this->pdo = new PDO("sqlite:".$rootDir.self::DATABASE_PATH);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Exception: '".$e->getMessage()."' at line ".$e->getLine();
}

However each time I run config.php I get the following error:

Exception: 'could not find driver' at line 22

This is the line where I initialise a PDO.


I have tried lots of solutions:

  • Purging and reinstalling php-common, php-sqlite3, and others
  • Uncommenting these lines in my php.ini file (in /etc/php/7.2/cli):
extension=pdo_sqlite
extension=sqlite3
  • Uncommenting and setting an extension directory in the same php.ini file:
sqlite3.extension_dir = "ext"
  • Restarting the apache2 service (sudo service apache2 restart)

When I run php -m I do get the modules in the list, but with a warning:

$ php -m
PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_sqlite' (tried: /usr/lib/php/20170718/pdo_sqlite (/usr/lib/php/20170718/pdo_sqlite: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/pdo_sqlite.so (/usr/lib/php/20170718/pdo_sqlite.so: undefined symbol: php_pdo_unregister_driver)) in Unknown on line 0
PHP Warning:  Module 'sqlite3' already loaded in Unknown on line 0
[PHP Modules]
...
PDO
pdo_sqlite
...
sqlite3
...

But when I look into the contents of the directory, those files seem to be present:

$ ls -al /usr/lib/php/20170718
total 6372
drwxr-xr-x 2 root root    4096 Mar  1 11:40 .
drwxr-xr-x 4 root root    4096 Mar  1 11:38 ..
...
-rw-r--r-- 1 root root  113048 Feb 11 15:55 pdo.so
-rw-r--r-- 1 root root   31096 Feb 11 15:55 pdo_sqlite.so
...
-rw-r--r-- 1 root root   52648 Feb 11 15:55 sqlite3.so
...

When I look into its parent directory, I see:

$ ls -al /usr/lib/php/
total 40
drwxr-xr-x   4 root root 4096 Mar  1 11:38 .
drwxr-xr-x 144 root root 4096 Mar  1 11:38 ..
drwxr-xr-x   2 root root 4096 Mar  1 11:40 20170718
drwxr-xr-x   3 root root 4096 Mar  1 11:38 7.2
-rw-r--r--   1 root root 4845 Jan 17  2018 php-helper
-rw-r--r--   1 root root 9534 Jan 17  2018 php-maintscript-helper
-rwxr-xr-x   1 root root 2922 Jan 17  2018 sessionclean

Could the issue be because of both 20170718 and 7.2 are present (I'm not really sure of the difference)? The 7.2 directory doesn't have any of the .so files though.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125

2 Answers2

0

On restarting my computer (and thus the IDE I am using, IntelliJ), I received a different error when running the config.php file - that I needed to install php-cgi.

I did so and no longer received the exception when running config.php. I suppose I already solved that problem and it just required an IDE restart to apply it.


The second issue, that still remained, was the warning:

PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_sqlite' (tried: /usr/lib/php/20170718/pdo_sqlite (/usr/lib/php/20170718/pdo_sqlite: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/pdo_sqlite.so (/usr/lib/php/20170718/pdo_sqlite.so: undefined symbol: php_pdo_unregister_driver)) in Unknown on line 0
PHP Warning:  Module 'sqlite3' already loaded in Unknown on line 0

And I resolved this using the answer here:

It means there is an extension=... or zend_extension=... line in one of your php configuration files (php.ini, or another close to it) that is trying to load that extension.

In trying to find a solution earlier, I had uncommented a few lines in the php.ini file. Simple commenting them back fixed the issue!

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
-1

In case of php 7.4 on Windows, you must include the directory where libsqlite3.dll resides in the path.

See: https://www.php.net/manual/en/sqlite3.installation.php

  • 1
    Please share more details. The given file structure lets me think that Windows is in no way part of the problem – Nico Haase Sep 29 '21 at 11:15
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29950955) – Toni Sep 29 '21 at 14:54