SQLite execute()通过Apache工作但不通过PHP CLI工作?

I have a query that I am running in PHP 5.5.26 to access some information in a SQLite database:

$query = 'SELECT `filename` ';
$query .= 'FROM `history` ';
$stmt = $link->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$filename = $row['filename'];

So through Apache I can view the script in my browser and get a result, it works.

If I try to run this script using PHP CLI from a shell:

php script.php

I get:

PHP Fatal error:  Call to a member function execute() 
on a non-object in script.php on line 4

Line 4 is: $stmt->execute();

So through the browser client and Apache it works but when I try to do this via CLI it does not.

Usually CLI will use a different php.ini file to what is being used by Apache but in my case and because of the device in use CLI uses the same php.ini file as Apache. So when I do php --ini via CLI I get:

Configuration File (php.ini) Path: /etc/php/apache2-php5
Loaded Configuration File:         /etc/php/apache2-php5/php.ini

Indeed, it's the same php.ini file that Apache is using. When I evoke phpinfo via CLI php -i it gives me the following which stands out:

PDO

PDO support => enabled
PDO drivers => mysql, sqlite

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => 5.5.43-MariaDB

Directive => Local Value => Master Value
pdo_mysql.default_socket => /var/lib/mysql/mysql.sock => /var/lib/mysql/mysql.sock

pdo_sqlite

PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.8.6


sqlite3

SQLite3 support => enabled
SQLite3 module version => 0.7-dev
SQLite Library => 3.8.6

Directive => Local Value => Master Value
sqlite3.extension_dir => no value => no value

So SQLite is installed and enabled, the PDO module for SQLite is installed and enabled.

And this is now where I am stuck. Why would it work in Apache but not work via PHP CLI? Any ideas or suggestions would be greatly appreciated, thank you in advance!