-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBManager.php
More file actions
executable file
·177 lines (166 loc) · 5.26 KB
/
DBManager.php
File metadata and controls
executable file
·177 lines (166 loc) · 5.26 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class DBManager
{
private $serveraddr;
private $username;
private $password;
private $dbname;
private $dbconnect;
private $tablename;
public function __construct(string $serveraddr, string $username, string $password)
{
$this->serveraddr = $serveraddr;
$this->username = $username;
$this->password = $password;
}
public function IsConnected()
{
if($this->dbconnect == null)
return false;
else
return true;
}
public function Connect(string $dbname)
{
try{
$this->dbconnect = new PDO("mysql:host=" . $this->serveraddr . ";dbname=" . $dbname, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
$this->dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbname = $dbname;
return true;
}
catch(PDOException $e)
{
#echo $e->getMessage();
$this->dbconnect = null;
return false;
}
}
public function CreateDatabase($dbname, $dbcmd)
{
try{
$this->dbconnect = new PDO("mysql:host=" . $this->serveraddr, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
$this->dbconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlcmd = "CREATE DATABASE IF NOT EXISTS $dbname $dbcmd;";
$this->dbconnect->exec($sqlcmd);
$this->dbconnect->exec("use $dbname;");
$this->dbname = $dbname;
return true;
}
catch(PDOException $e)
{
// echo $e->getMessage();
$dbconnect = null;
return false;
}
}
public function CreateTable($tablename, $tablecmd)
{
try{
$sqlcmd = "CREATE TABLE IF NOT EXISTS "
. $tablename
. $tablecmd;
if($this->ExecCmd($sqlcmd))
return true;
else
return false;
}
catch(PDOException $e)
{
echo $e->getMessage();
$this->dbconnect = null;
return false;
}
}
public function ExecCmd($cmd)
{
try{
if(!$this->IsConnected())
return false;
$this->dbconnect->beginTransaction();
$this->dbconnect->exec($cmd);
$this->dbconnect->commit();
return true;
}
catch(PDOException $e)
{
$this->dbconnect->rollback();
echo $cmd . "<br>" . $e->getMessage();
return false;
}
}
public function Select(string $table, string $where = "", bool $countonly = true)
{
try{
if(!$this->IsConnected())
return false;
$cmd = "select * from $table where $where";
$results = $this->dbconnect->query($cmd);
if($countonly){
$count = $results->rowCount();
$results->closeCursor();
return $count;
}
$resultarray = [];
foreach ($results as $row) {
array_push($resultarray, $row);
}
return $resultarray;
}
catch(PDOException $e)
{
$this->dbconnect->rollback();
echo $cmd . "<br>" . $e->getMessage();
return false;
}
}
public function Insert(string $table, array $keypairs)
{
try{
if(!$this->IsConnected())
return false;
if(count($keypairs) < 1)
return false;
$keys = "";
$vals = "";
foreach($keypairs as $key => $val){
$vallen = strlen($val);
if(!($vallen > 2 && substr($val, -2) === "()"))
$val = "'$val'";
$keys = "$keys $key,";
$vals = "$vals $val,";
}
$keys = ltrim(rtrim($keys, ","));
$vals = ltrim(rtrim($vals, ","));
$cmd = "insert into $table ($keys) VALUES ($vals);";
$ret = $this->dbconnect->exec($cmd);
return $ret;
}
catch(PDOException $e)
{
echo $cmd . "\n" . $e->getMessage() . "\n";
return false;
}
}
public function Update(string $table, string $where, array $keypairs)
{
try{
if(!$this->IsConnected())
return false;
if(count($keypairs) < 1)
return false;
$updatepairs = "";
foreach($keypairs as $key => $val) {
$updatepairs = "$updatepairs $key = '$val',";
}
$updatepairs = rtrim($updatepairs, ",");
$cmd = "update $table set $updatepairs where $where;";
$ret = $this->dbconnect->exec($cmd);
return $ret;
}
catch(PDOException $e)
{
echo $cmd . "\n" . $e->getMessage() . "\n";
return false;
}
}
}