0

I wanted to connect a MySQL database to my Laravel application.
I created one using PHPMyAdmin administration tool of MySQL, then I added it in the .env as whereas the database.php application files.

I ran the terminal command: php artisan migrate which gave me the following error:

could not find driver (SQL: select * from information_schema.tables where table_schema = pl_project and table_name = migrations and table_type = 'BASE TABLE')
  at C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

  1   C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDOException::("could not find driver")

  2   C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDO::__construct()
Here is the .env excerpt that I edited:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pl_project
DB_USERNAME=root
DB_PASSWORD=
Here is the database.php excerpt that I edited:
'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'pl_project'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]
Here is the output of the command: php -m
[PHP Modules]
bcmath
calendar
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
Phar
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
A couple of notes:
  1. I did add PHP to my env variables as well as MySQL.
  2. I did uncomment extension=pdo_mysql in php.ini.

But none of it changed anything and I still get the same error.

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
  • Does this answer your question? [Laravel: PDOException: could not find driver](https://stackoverflow.com/questions/42557693/laravel-pdoexception-could-not-find-driver) – steven7mwesigwa Nov 18 '22 at 09:10
  • [PDOException “could not find driver”](https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver) – steven7mwesigwa Nov 18 '22 at 09:11
  • I did come across these 2 questions and none of the answers helped at all. – coraline98 Nov 18 '22 at 09:14
  • If you look closely at your output of the command: `php -m` in an effort to view currently enabled modules, you're **lucking** the extension `pdo_mysql`. Please, check to confirm if you're editing the *correct* `php.ini` file. – steven7mwesigwa Nov 18 '22 at 11:29
  • I checked all the php.ini files there were 2 so I edited the second one. restarted the server then tried to migrate again only to get the same error.. – coraline98 Nov 18 '22 at 11:47
  • Please, [edit your question](https://stackoverflow.com/posts/74487068/edit) and show us the output of the command: `$Env:Path` using the *'PowerShell'* application. – steven7mwesigwa Nov 18 '22 at 14:03

2 Answers2

0

There is no need in editing database.php file as it is using .env for database info. In case this is new project that did not have .env file, try running php artisan config:clear

Have you tried restarting PC after enabling extension? If you are running your project with artisan serve it will not reflect after you change ini file

I also had few similar issues on couple of machines with database, it got solved with commenting 'url' => env('DATABASE_URL'), line in mysql

Hope some of this helps

0

Comment:

I checked all the php.ini files there were 2 so I edited the second one. restarted the server then tried to migrate again only to get the same error.. – OP - https://stackoverflow.com/posts/comments/131493101?noredirect=1

From what I can recall, the default XAMPP installation lucks a php.ini file in the path xampp/php/.

It however has php.ini-development and php.ini-production configuration files in that path. You would normally have to create a duplicate copy of either of those two files depending on your use case and rename the 'duplicate copy' to php.ini This renamed file would then be considered as the 'active/default PHP configuration file'.

You would then open this new renamed php.ini file, uncomment any necessary extension libraries, save and restart your XAMPP web server (Apache).

Addendum

You may need to enable/uncomment the extension libraries below for your case.

mysqli

pdo_mysql

pdo_sqlite
Addendum 2

A. Enable/uncomment the line below. Change the path to your XAMPP installation path. I.e:

extension_dir="C:\xampp\php\ext"
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
  • I did try that right now and it's still not working. the extension is uncommented but for some reason it won't install it! – coraline98 Nov 18 '22 at 12:16
  • @coraline98 Try uncommenting these two extension libraries as well: `mysqli` & `pdo_sqlite` in addition to `pdo_mysql`. Secondly, restart the XAMPP web server. Lastly, open a **'new instance/window'** of the *'Command Prompt'* or *'PowerShell'* and confirm that all enabled extension libraries appear in the output list when you run the command: `php -m` – steven7mwesigwa Nov 18 '22 at 12:29
  • it did not work too. I tried checking the `extension_dir` it was commented so I uncommented it and tried to add the absolute path instead but still got the same result. – coraline98 Nov 18 '22 at 12:55
  • @coraline98 `php -m` Are all enabled extension libraries in the output list? – steven7mwesigwa Nov 18 '22 at 13:43
  • no. I uncommented another extension to enable it and it didn't show up when I used `php -m` – coraline98 Nov 18 '22 at 13:55