This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBManager_mysqli.php
113 lines (99 loc) · 2.19 KB
/
DBManager_mysqli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
class DBManager
{
public $link;
public $host;
public $username;
public $password;
public $dbname;
function __construct($host,$username,$password, $dbname)
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->connect();
}
public function connect()
{
$this->link = new mysqli($this->host, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->link->connect_error)
{
die("Connection failed: " . $this->link->connect_error);
}
}
public function QuerySingleRow()
{
$args = func_get_args();
$sql = $args[0];
foreach ($args as $key => $arg) {
if($key >= 1) {
$arg = $this->link->real_escape_string($arg);
$sql = str_replace("%a".($key-1), $arg, $sql);
}
}
$result = $this->link->query($sql);
if($result !== false) {
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
//$this->link->close();
return $row;
}
}
// $this->link->close();
return null;
}
public function insert() {
$args = func_get_args();
$sql = $args[0];
foreach ($args as $key => $arg) {
if($key >= 1) {
$arg = $this->link->real_escape_string($arg);
$sql = str_replace("%a".($key-1), $arg, $sql);
}
}
$result = $this->link->query($sql);
return ($result === TRUE);
}
public function delete()
{
$args = func_get_args();
$sql = $args[0];
foreach ($args as $key => $arg) {
if($key >= 1) {
$arg = $this->link->real_escape_string($arg);
$sql = str_replace("%a".($key-1), $arg, $sql);
}
}
$result = $this->link->query($sql);
return ($result === TRUE);
}
public function query()
{
$args = func_get_args();
$sql = $args[0];
foreach ($args as $key => $arg) {
if($key >= 1) {
$arg = $this->link->real_escape_string($arg);
$sql = str_replace("%a".($key-1), $arg, $sql);
}
}
$result = $this->link->query($sql);
$res = array();
if($result !== false) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($res, $row);
}
} else {
$res = null;
}
} else {
$res = null;
}
//$this->link->close();
return $res;
}
}
?>