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.