2

I am deploying a Laravel project to a shared hosting and added a php.ini with:

extension=pdo.so extension=pdo_sqlite.so extension=sqlite.so 

In phpinfo, I could also see that the sqlite extensions are loaded. Testing to connect to the database with the following code snippets also worked:

<?php try { $dbh = new PDO("sqlite:app/database/production.sqlite"); echo "Connected to database!"; } catch(PDOException $e) { echo $e->getMessage(); } 

However, when trying to connect to the databse using Laravel, the application throws me the PDOException error.

Appreciate any help on this. Thanks in advance!

8
  • 1
    Have you done the steps from How to determine if PDO is enabled in PHP? - Especially in this anser: stackoverflow.com/a/6113496/367456. If not please do them. Then or if yes, provide the results in your question. This is important to understand what makes your question different from existing ones about sqlite and with the same error message. Commented Dec 26, 2014 at 16:22
  • Using extension_loaded('pdo') and extension_loaded('pdo_sqlite') returned true. Is there any other place I could check? It seems that only the laravel application is complaining about not finding the driver. Commented Dec 26, 2014 at 17:08
  • Those results look promising to me, the drivers are loaded. Next point I would check is if the sqlite-database-file can be found. Please provide the absolute path of app/database/production.sqlite from the root directory onwards starting with /. Commented Dec 26, 2014 at 17:20
  • The path to the database is located in /public_html/mockup/app/database/production.sqlite and the path in database.php is DIR.'/../database/production.sqlite' I have no issues when viewing the website on my local machine. Commented Dec 26, 2014 at 17:25
  • Yes, it's not the filename, just seeing. Commented Dec 26, 2014 at 17:27

1 Answer 1

3

The exception you get:

PDOException: Could not find driver

is given by PHP's PDO whenever the driver to your database could not be found. PDO needs a driver to operate with different databases (e.g. Sqlite, Mysql, ...). And PDO looks for the driver based on the DSN. In your case the DSN is:

sqlite:app/database/production.sqlite 

And for the driver is looked only in the part before the first colon (":"):

sqlite 

The string sqlite is used to find the sqlite3 driver (see PDO_SQLITE DSN in the PHP manual).

There is only a single place in all PDO where this exception is thrown (php 5.4 lxr) and it only gets thrown in case for the text before the colon in the DSN no driver could be found. This is also what you can expect from the error message.

As you've already done the checks outlined in the best answer to "How to determine if PDO is enabled in PHP" you are already certain that the PDO sqlite extension has been loaded.

Just to make this clear: The PHP extension which contains the PDO driver for sqlite is named pdo_sqlite which you have checked to be loaded.

The only explanation I have is that you did load the extension but PHP was not able to load the driver. According to PHP sources (php 5.4 lxr) this can have exactly two reasons:

  1. The PDO driver requires a different PDO API version
  2. PDO must be loaded before loading any PDO drivers

(technically there is a third reason but it should be ignored: adding the driver to the hashtable of PDO drivers failed, this is pretty internal and has nothing to do specifically with PDO)

As the extension is loaded, but the driver is not found, I suspect there was a problem registering the PDO driver. You should checkout the PHP error log for startup errors. As you say you have this problem on a shared hoster you need to contact the support where you find the PHP error log. You're looking for startup errors. Sometimes these error are also shown in the error log of the webserver depending on which PHP SAPI interface is used.

Please also provide the information from phpinfo() so that additional guidance can be given. With the information you've provided you can rest assured that the sqlite driver for PDO has not been loaded. This is a configuration issue.

Sign up to request clarification or add additional context in comments.

3 Comments

In the configure command, there are '--with-pdo-sqlite=shared' and '--with-sqlite=shared' listed. Also, phpinfo() shows that the PDO drivers for sqlite is enabled and the SQLite Library is listed as 3.7.7.1 I will try to see if I can get my hands on the PHP error log.
A senior tech support for the shared hosting has included a default php.ini and uncommented the lines that I have included in my first post and the page loads fine. I am not sure what has been done, but I will keep this ini file for further use.
The extensions are most likely configured on the server itself. You don't need to load extension in your own php.ini. Consult your hosters PHP documentation on how to configure extensions and which extensions are available by default for more information. - Still I don't understand how this has prevented the driver from being found. Perhaps the driver has been loaded twice?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.