Start on project with the connection services and first command to list databases

This commit is contained in:
Tim
2021-09-19 11:00:41 +02:00
parent 5e2bc3505f
commit 2fd43ed717
7 changed files with 161 additions and 59 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace App\Service;
use mysqli;
class DatabaseManager
{
private mysqli $conn;
public function __construct(
private MysqliConnection $service
)
{
$this->conn = $this->service->getConnection();
}
public function listDatabases(): array
{
$dbs = [];
$results = $this->conn->query('SHOW DATABASES');
while ($result = $results->fetch_object()) {
$dbs[] = $result->Database;
}
return $dbs;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Service;
use mysqli;
class MysqliConnection
{
private mysqli $connection;
public function __construct($hostname, $user, $password)
{
$this->connection = new mysqli($hostname, $user, $password);
if ($this->connection->connect_error) {
throw new \Exception("Connection failed: " . $this->connection->connect_error);
}
}
public function getConnection(): mysqli
{
return $this->connection;
}
}