Reputation: 545
I'm writing a small tool for which it is necessery to check if database-credentials are valid and working. The tool is based on laravel. The credentials are submitted via a post-route.
$mysql2 = array(
'driver' => 'mysql',
'host' => 'localhosts',
'database' => 'test',
'username' => 'testUser',
'password' => 'testPasswort',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
);
DB::connection($mysql2);
I hoped I could do something like this code above. But I can't get it working. Has anyone ever attempted something similar?
Edit1:
I tried switching the credentials in the config file:
Config::set("database.connections.mysql", [
"host" => "localhast",
"database" => "test",
"username" => "testUser",
"password" => "testPasswort"
]);
$con = DB::connection()->getPdo();
Sadly there is still some error thrown.
Upvotes: 1
Views: 702
Reputation: 17206
Have you tried using a new connection (not editing the already existing one)
$newName = uniqid('db'); //example of unique name
Config::set("database.connections.".$newName, [
"host" => "localhast",
"database" => "test",
"username" => "testUser",
"password" => "testPasswort"
]);
try {
DB::connection($newName)->getPdo();
} catch (\Exception $e) {
//handle error
}
Upvotes: 1
Reputation: 859
You don't need to pass the array to check DB connection just simply apply this code you will get to know about connection. This will automatically take the .env file connection variables.
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
Upvotes: 0