-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
45 lines (37 loc) · 1.17 KB
/
Database.php
File metadata and controls
45 lines (37 loc) · 1.17 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
<?php
namespace MyApp;
class Database {
private $connection;
public function __construct($config) {
$this->connection = new \mysqli(
$config['host'],
$config['user'],
$config['pass'],
$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;
}
}