skip to Main Content

Episode is about hiding passwords.

I receive an error refreshing localhost – ‘could not find driver’ after making config.php as shown in the video.

Connection.php

<?php

class Connection
{
    public static function make($config)
    {
        try {
/*            return new PDO('mysql:host=localhost:3306;dbname=schema_test', 'name', 'password');*/
            return new PDO(
                $config['connection'].';dbname='.$config['name'],
                $config['username'],
                $config['password'],
                $config['options']
            );
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }
}

It worked with the commented out version, but after making config.php and taking the information from there is when I receive the error.

config.php

<?php

return [
    'database' => [
        'name' => 'schema_test',
        'username' => 'name',
        'password' => 'password',
        'connection' => 'localhost:3306',
        'options' => [],
    ]
];

bootstrap.php

<?php

require 'database/Connection.php';

require 'database/QueryBuilder.php';

$config = require 'config.php';

return new QueryBuilder(
    Connection::make($config['database'])
);

Also I get no additional information about the error in the console.

I’ve already tried solutions, which worked for others, like:
sudo apt-get install php-mysql

I tried reinstalling a few php related things and that didn’t change anything either.

<?php

require 'database/Connection.php';

require 'database/QueryBuilder.php';

$config = require 'config.php';

print_r($config);

return new QueryBuilder(
    Connection::make($config['database'])
);

Output:

Array ( [database] => Array ( [name] => schema_test => user [password] => password [connection] => localhost:3306 [options] => Array ( ) ) ) SQLSTATE[HY000] [1045] Access denied for user ‘user’@’localhost’ (using password: YES)

2

Answers


  1. In your config.php, set directly the array :

    <?php
    
    $config = [
        'database' => [
            'name' => 'schema_test',
            'username' => 'name',
            'password' => 'password',
            'connection' => 'mysql:host=localhost:3306',
            'options' => [],
        ]
    ];
    

    In your bootstrap.php, please require your config.php file like that :

    require 'config.php';
    

    And do not forget to use $config[‘database’][‘name’]…

    Login or Signup to reply.
  2. You are not declaring the driver, thus you get 'could not find driver'. Also, you are missing to name the host= parameter.

    To correct this error change your PDO instantiation like this:

    return new PDO(
        'mysql:host='.$config['connection'].';dbname='.$config['name'],
        $config['username'],
        $config['password'],
        $config['options']
     );
    

    Your $config array is okay as it is, and it is also included correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search