-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBController.php
86 lines (69 loc) · 2.02 KB
/
DBController.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
81
82
83
84
85
86
<?php
class DBController
{
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "shopcartpaypal";
private static $conn;
function __construct()
{
$this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->database);
}
public static function getConnection()
{
if (empty($this->conn)) {
new Database();
}
}
function getDBResult($query, $params = array())
{
$sql_statement = $this->conn->prepare($query);
if (! empty($params)) {
$this->bindParams($sql_statement, $params);
}
$sql_statement->execute();
$result = $sql_statement->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$resultset[] = $row;
}
}
if (! empty($resultset)) {
return $resultset;
}
}
function insertDB($query, $params = array())
{
$sql_statement = $this->conn->prepare($query);
if (! empty($params)) {
$this->bindParams($sql_statement, $params);
}
$sql_statement->execute();
$id = mysqli_insert_id ( $this->conn );
return $id;
}
function updateDB($query, $params = array())
{
$sql_statement = $this->conn->prepare($query);
if (! empty($params)) {
$this->bindParams($sql_statement, $params);
}
$sql_statement->execute();
}
function bindParams($sql_statement, $params)
{
$param_type = "";
foreach ($params as $query_param) {
$param_type .= $query_param["param_type"];
}
$bind_params[] = & $param_type;
foreach ($params as $k => $query_param) {
$bind_params[] = & $params[$k]["param_value"];
}
call_user_func_array(array(
$sql_statement,
'bind_param'
), $bind_params);
}
}