-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
48 lines (39 loc) · 1.2 KB
/
Database.php
File metadata and controls
48 lines (39 loc) · 1.2 KB
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
<?php
namespace MyApp;
use Exception;
use mysqli;
class Database {
private $connection;
public function __construct($config) {
$this->connection = new mysqli(
$config['host'],
$config['user'],
$config['password'],
$config['dbname']
);
if ($this->connection->connect_error) {
throw new Exception('Connection failed: ' . $this->connection->connect_error);
}
}
public function query($query, $params = []) {
$stmt = $this->connection->prepare($query);
if ($params) {
$stmt->bind_param(...$params);
}
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
public function count($table) {
$result = $this->query("SELECT COUNT(*) as count FROM $table");
return $result[0]['count'];
}
public function getColumns($table) {
$result = $this->query("SHOW COLUMNS FROM $table");
return array_column($result, 'Field');
}
public function tableExists($table) {
$result = $this->query("SHOW TABLES LIKE ?", ['s', $table]);
return count($result) > 0;
}
}