Episode is about hiding passwords.
I receive an error refreshing localhost – ‘could not find driver’ after making config.php as shown in the video.
<?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.
<?php
return [
'database' => [
'name' => 'schema_test',
'username' => 'name',
'password' => 'password',
'connection' => 'localhost:3306',
'options' => [],
]
];
<?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
In your config.php, set directly the array :
In your bootstrap.php, please require your config.php file like that :
And do not forget to use $config[‘database’][‘name’]…
You are not declaring the driver, thus you get
'could not find driver'
. Also, you are missing to name thehost=
parameter.To correct this error change your PDO instantiation like this:
Your
$config
array is okay as it is, and it is also included correctly.