This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connector.php
80 lines (62 loc) · 1.78 KB
/
Connector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Database;
use mysqli;
use mysqli_stmt;
use RuntimeException;
class Connector
{
private static ?Connector $instance = null;
private $connection;
private function __construct()
{
$configuration = Configuration::getInstance();
$this->connection = new mysqli(
$configuration->db_host,
$configuration->db_user,
$configuration->db_password,
$configuration->db_name,
$configuration->db_port
);
if ($this->connection->connect_errno) {
throw new RuntimeException("Erreur de connexion mysqli: " . $this->connection->connect_error);
}
}
public static function getInstance(): Connector
{
if (Connector::$instance === null) {
Connector::$instance = new Connector();
}
return Connector::$instance;
}
public function createStatement(string $query): mysqli_stmt
{
return new mysqli_stmt($this->connection, $query);
}
public function query(string $query)
{
return $this->connection->query($query);
}
public function multiQuery(string $query)
{
$success = $this->connection->multi_query($query);
if (!$success) {
return null;
}
$results = [];
do {
$store_result = $this->connection->store_result();
if ($store_result) {
$row = $store_result->fetch_row();
while ($row) {
$results[] = $row;
$row = $store_result->fetch_row();
}
}
} while ($this->connection->next_result());
return array_filter($results);
}
public function close()
{
$this->connection->close();
}
}