-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.class.php
54 lines (39 loc) · 1.24 KB
/
Database.class.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
<?php
class Database {
private static $instance = null;
private $connection;
private $database;
private function __construct($database) {
$this->connection = new PDO(SQL_TYPE . ':host=' . SQL_HOST . ';dbname=' . $database, SQL_USER, SQL_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
public static function Sql($database = SQL_DB) {
if(!isset(self::$instance) || self::$instance->database != $database) {
self::$instance = new Database($database);
}
self::$instance->database = $database;
return self::$instance;
}
public function doQuery($sql, $params = []) {
$query = $this->connection->prepare($sql);
if(count($params)) {
foreach($params as $key => $value) {
$query->bindValue($key + 1, $value);
}
}
$query->execute();
return $query;
}
public function doSelect($sql, $params = []) {
$query = $this->doQuery($sql, $params);
return $query->fetchAll(PDO::FETCH_ASSOC);
}
public function doInsert($sql, $params = []) {
$query = $this->doQuery($sql, $params);
return $this->connection->lastInsertId();
}
public function doUpdate($sql, $params = []) {
$query = $this->doQuery($sql, $params);
return $query->rowCount();
}
}
?>