-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnect.class.php
105 lines (96 loc) · 2.7 KB
/
Connect.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
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
<?php
if (!defined("webStart")) {
exit(0);
}
class Connect
{
static private $mysqli;
static private function _con()
{
$db_config = array(
"host" => "localhost",
"user" => "root",
"pass" => "",
"dbname" => "webwat",
"charset" => "utf8"
);
Connect::$mysqli = @new mysqli($db_config["host"], $db_config["user"], $db_config["pass"], $db_config["dbname"]);
Connect::$mysqli->set_charset($db_config["charset"]);
}
static function select($table, $where)
{
Connect::_con();
$arr = array();
$sql = "SELECT * FROM $table $where";
$result = Connect::$mysqli->query($sql);
if (!$result) {
die("SQL Error: <br>" . $sql . "<br>" . Connect::$mysqli->error);
}
while($data= $result->fetch_assoc()) {
$arr[]=$data;
}
Connect::$mysqli->close();
return $arr;
}
static function insert($table, $data)
{
Connect::_con();
$fields = "";
$values = "";
$i = 1;
foreach ($data as $key => $val) {
if ($i != 1) {
$fields .= ", ";
$values .= ", ";
}
$fields .= "$key";
$values .= "'$val'";
$i++;
}
$sql = "INSERT INTO $table ($fields) VALUES ($values)";
if (Connect::$mysqli->query($sql)) {
Connect::$mysqli->close();
return true;
} else {
die("SQL Error: <br>" . $sql . "<br>" . Connect::$mysqli->error);
return false;
}
}
static function update($table, $data, $where)
{
Connect::_con();
$modifs = "";
$i = 1;
foreach ($data as $key => $val) {
if ($i != 1) {
$modifs .= ", ";
}
if (is_numeric($val)) {
$modifs .= $key . '=' . $val;
} else {
$modifs .= $key . ' = "' . $val . '"';
}
$i++;
}
$sql = ("UPDATE $table SET $modifs $where");
if (Connect::$mysqli->query($sql)) {
Connect::$mysqli->close();
return true;
} else {
die("SQL Error: <br>" . $sql . "<br>" . Connect::$mysqli->error);
return false;
}
}
static function delete($table, $where)
{
Connect::_con();
$sql = "DELETE FROM $table $where";
if (Connect::$mysqli->query($sql)) {
Connect::$mysqli->close();
return true;
} else {
die("SQL Error: <br>" . $sql . "<br>" . Connect::$mysqli->error);
return false;
}
}
}