-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePDO.php
More file actions
156 lines (143 loc) · 3.78 KB
/
Copy pathSimplePDO.php
File metadata and controls
156 lines (143 loc) · 3.78 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
<?php
/***********************************************************
*
* SimplePDO
*
* Version 1.0
*
* Masoud HassanBeygi
*
* http://masoudhb.ir/
*
* https://github.com/masoudhb/
*
**********************************************************/
class SimplePDO {
protected $db;
function __construct($dbname, $dbuser, $dbpass) {
try {
$this->db = new PDO("mysql:host=localhost;dbname=$dbname;charset=utf8","$dbuser","$dbpass");
$this->db->query("SET NAMES utf8");
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (Exception $e) {
die($e->getMessage());
}
}
private static function where($query, $where) {
$query .= " WHERE ";
$where_array = array();
foreach ($where as $item) {
if (is_array($item)) {
if (array_key_exists($item[0], $where_array)) {
$val = $item[0].rand(100,999);
$query .= "$item[0]$item[1]:$val ";
$where_array[$val] = $item[2];
} else {
$query .= "$item[0]$item[1]:$item[0] ";
$where_array[$item[0]] = $item[2];
}
} else {
$query .= "$item ";
}
}
$query = trim($query);
$data['query'] = $query;
$data['where_array'] = $where_array;
return $data;
}
public function rows($table, $where = '') {
if ($table) {
$query = "SELECT * FROM $table";
if (is_array($where)) {
$dowhere = SimplePDO::where($query, $where);
$query = $dowhere['query'];
}
$stmt = $this->db->prepare($query);
if ($stmt) {
if (is_array($where) && is_array($dowhere['where_array'])) {
$stmt->execute($dowhere['where_array']);
} else {
$stmt->execute();
}
$rowcount = $stmt->rowcount();
return $rowcount;
}
}
return false;
}
public function select($table, $what = "*", $where = '', $order = '', $limit = '') {
if ($table) {
$query = "SELECT $what FROM $table";
if (is_array($where)) {
$dowhere = SimplePDO::where($query, $where);
$query = $dowhere['query'];
}
if ($order) {
$query .= " ORDER BY $order[0] $order[1]";
}
if ($limit) {
$query .= " LIMIT $limit";
}
$stmt = $this->db->prepare($query);
if ($stmt) {
if (is_array($where) && is_array($dowhere['where_array'])) {
$stmt->execute($dowhere['where_array']);
} else {
$stmt->execute();
}
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
return $result;
}
}
return false;
}
public function insert($table, $data) {
if ($table && is_array($data)) {
$query = "INSERT INTO $table SET ";
$insert_array = array();
foreach ($data as $key => $value) {
$query .= "$key=:$key, ";
$insert_array[$key] = $value;
}
$query = rtrim($query, ", ");
$stmt = $this->db->prepare($query);
if ($stmt) {
$stmt->execute($insert_array);
return $this->db->lastInsertId();
}
}
return false;
}
public function update($table, $data, $where) {
if ($table && is_array($data) && is_array($where)) {
$query = "UPDATE $table SET ";
$update_array = array();
foreach ($data as $key => $value) {
$query .= "$key=:$key, ";
$update_array[$key] = $value;
}
$query = rtrim($query, ", ");
$dowhere = SimplePDO::where($query, $where);
$final_array = array_merge($update_array, $dowhere['where_array']);
$stmt = $this->db->prepare($dowhere['query']);
if ($stmt) {
$stmt->execute($final_array);
return true;
}
}
return false;
}
public function delete($table, $where) {
if ($table && is_array($where)) {
$query = "DELETE FROM $table";
$dowhere = SimplePDO::where($query, $where);
$stmt = $this->db->prepare($dowhere['query']);
if ($stmt) {
$stmt->execute($dowhere['where_array']);
return true;
}
}
return false;
}
}
?>