This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JDB.php
145 lines (111 loc) · 3.81 KB
/
JDB.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
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
<?php
class Jdb {
protected $link;
public function __construct($db = _DB_NAME, $host = _DB_HOST, $user = _DB_USER, $password = _DB_PASSWD, $socket = _DB_SOCK) {
$this->link = mysqli_connect($host, $user, $password, $db, 0, $socket) or print("Error " . mysqli_error($this->link));
if ($this->link->connect_errno > 0) {
die('DB_ERROR [' . $this->link->connect_error . ']');
}
if (!$this->link) {
var_dump($this->link->error);
die("DB_ERROR:" . mysqli_error($this->link));
}
$this->link->query("SET character_set_client=utf8");
$this->link->query("SET character_set_connection=utf8");
$this->link->query("SET character_set_results=utf8");
}
private function getResult($sQuery) {
$sQuery = <<<SQL
$sQuery
SQL;
if (!$result = $this->link->query($sQuery)) {
die('DB_ERROR [' . $this->link->error . ']' . ' STMT :' . $sQuery);
}
return $result;
}
public function getRows( $sTable, $aKeys, $aLimit = [], $cond = 'AND') {
$sStmt = 'SELECT * FROM `' . $sTable . '` WHERE ';
$sCond = '';
foreach ($aKeys as $key => $value) {
$sCond .= '`' . $key . '` = "' . $value . '" '.$cond;
}
$pResult = $this->getResult(substr($sStmt . $sCond, 0, -3));
$aReturn = [];
while ($aRow = $pResult->fetch_assoc()) {
//var_dump($aRow);
$aReturn[] = $aRow;
}
$pResult->free();
return $aReturn;
}
public function getRowsByQuery( $query ) {
$pResult = $this->getResult($query);
$aReturn = [];
while ($aRow = $pResult->fetch_assoc()) {
//var_dump($aRow);
$aReturn[] = $aRow;
}
$pResult->free();
return $aReturn;
}
public function getRow($sQuery) {
$pResult = $this->getResult($sQuery);
$aRow = $pResult->fetch_assoc();
$pResult->free();
//var_dump($aRow);
return $aRow;
}
public function updateInsert($sTable, $aKeys, $aData) {
$aData = array_merge($aData,$aKeys);
$sCond = '';
$sStmt = 'SELECT count(*) as count FROM `' . $sTable . '` WHERE ';
foreach ($aKeys as $key => $value) {
$sCond .= '`' . $key . '` = "' . $value . '" AND';
}
$aRow = $this->getRow(substr($sStmt . $sCond, 0, -3));
//var_dump(substr($sStmt . $sCond, 0, -3), $aRow);
if ($aRow['count'] > 0) {
//UPDATE
$sStmt = 'UPDATE `' . $sTable . '` SET ';
foreach ($aData as $key => $value) {
$sStmt .= ' `' . $key . '` = "' . $value . '",';
}
$sStmt = substr($sStmt, 0, -1) . ' WHERE ' . $sCond;
//var_dump(substr( $sStmt ,0,-3));
$this->getResult(substr($sStmt, 0, -3));
//TODO UNSAFE
} else {
//INSERT
foreach ($aData as $key => $value)
$aData[$key] = "'$aData[$key]'";
$sStmt = 'INSERT INTO `' . $sTable . '` (' . implode(array_keys($aData), ' ,') . ') VALUES (' . implode(array_values($aData), ' ,') . ') ';
$this->getResult($sStmt);
}
return $aRow;
}
public function insert($sTable, $aData){
foreach ($aData as $key => $value)
$aData[$key] = "'$aData[$key]'";
$sStmt = 'INSERT INTO `' . $sTable . '` (`' . implode(array_keys($aData), '` ,`') . '`) VALUES (' . implode(array_values($aData), ' ,') . ') ';
return $this->getResult($sStmt);
}
public function update($sTable, $aKeys, $aData) {
global $bDebug;
$sCond = '';
foreach ($aKeys as $key => $value) {
$sCond .= '`' . $key . '` = "' . $value . '" AND';
}
//UPDATE
$sStmt = 'UPDATE `' . $sTable . '` SET ';
foreach ($aData as $key => $value) {
$sStmt .= ' `' . $key . '` = "' . $value . '",';
}
$sStmt = substr($sStmt, 0, -1) . ' WHERE ' . $sCond;
//var_dump(substr( $sStmt ,0,-3));
$this->getResult(substr($sStmt, 0, -3));
//TODO UNSAFE
if ($bDebug)
return substr($sStmt, 0, -3);
}
}
$objDb = new Jdb(_DB_NAME, _DB_HOST, _DB_USER, _DB_PASSWD, _DB_SOCK);