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!

hakre
  • 193,403
  • 52
  • 435
  • 836
Terry
  • 41
  • 1
  • 5
  • 1
    Have you done the steps from [How to determine if PDO is enabled in PHP?](http://stackoverflow.com/q/6113262/367456) - Especially in this anser: http://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. – hakre Dec 26 '14 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. – Terry Dec 26 '14 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 `/`. – hakre Dec 26 '14 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. – Terry Dec 26 '14 at 17:25
  • Yes, it's not the filename, just seeing. – hakre Dec 26 '14 at 17:27
  • Can you run a `phpinfo();` and report back which sqlite version for PDO is loaded? The phpinfo should list under PDO all PDO drivers. – hakre Dec 26 '14 at 17:48
  • In your Laravel `app/config/database.php` do you have the default connection set to `sqlite`? The exception doesn't specify which PDO driver is missing. So you might have the default connection set to `mysql`, but `pdo_mysql` might not be is not be enabled/installed (so you'd get this error because the default connection is trying to use another PDO extension). – Bogdan Dec 26 '14 at 21:31
  • Try to do this. Just try. For ubuntu, If mac use something else. If windows, change OS. sudo apt-get install php5-sqlite – atilkan Sep 10 '15 at 03:54

1 Answers1

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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. – Terry Dec 26 '14 at 18:38
  • 1
    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. – Terry Dec 27 '14 at 14:43
  • 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? – hakre Dec 27 '14 at 14:45