forked from mosfeqanik/content_management_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
228 lines (170 loc) · 4.81 KB
/
Database.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
class Database{
public $a;
private $b;
protected $c;
protected $conn;
public function __destruct(){
//echo "Bye Bye...";
}
public function testPublic($a){
echo "I'm from Public<br>";
echo $this->a;
//$this->testPrivate();
}
public function __construct($host,$user,$pass,$db){
//echo "GoodGood";
$this->conn=new mysqli($host,$user,$pass,$db);
//echo $this->conn->errno;
if($this->conn->connect_errno){
die($this->conn->connect_error);
}
}
public function getAll($table,$cols){
$sql="SELECT $cols FROM $table";
$result=$this->conn->query($sql);
if($result->num_rows>0){
return $result->fetch_all(MYSQLI_ASSOC);
}
else{
return false;
}
}
public function getAllWithCondition($table,$cols,$condition){
$sql="SELECT $cols FROM $table WHERE $condition";
$result=$this->conn->query($sql);
if($result->num_rows>0){
return $result->fetch_all(MYSQLI_ASSOC);
}
else{
return false;
}
}
public function getAllwithTable($table,$cols,$css_classes="",$insert_file="insert.php",$edit_file="edit.php",$delete_file="delete.php"){
$sql="SELECT $cols FROM $table";
$result=$this->conn->query($sql);
if($result->num_rows>0){
$data= $result->fetch_all(MYSQLI_ASSOC);
$table="<table border=\"1\" width=\"400\" cellpadding=\"5\" class=\"$css_classes\">";
$table.="<tr>";
foreach($data[0] as $key=>$val){
$table.="<th>".ucfirst($key)."</th>";
}
$table.="</tr>";
foreach($data as $row){
$table.="<tr>";
foreach($row as $val){
$table.="<td>$val</td>";
}
$table.="<td><a href=\"$edit_file?id=$row[id]\" class=\"btn btn-primary glyphicon glyphicon-pencil\"></a> <a href=\"$delete_file?delid=$row[id]\" class=\"btn btn-danger glyphicon glyphicon-remove\"></a></td>";
$table.="</tr>";
}
$table.="<tr>";
$table.="<td colspan=\"5\" class=\"text-right\"><a class=\"btn btn-info\" href=\"$insert_file\">Add New Student</a></td>";
$table.="</tr>";
$table.="</table>";
return $table;
}
else{
return false;
}
}
public function getAllByIds($table,$cols,$condition){
$sql="SELECT $cols FROM $table WHERE $condition";
$result=$this->conn->query($sql);
if($result->num_rows>0){
return $result->fetch_all(MYSQLI_ASSOC);
}
else{
return false;
}
}
public function getById($table,$cols,$condition){
$sql="SELECT $cols FROM $table WHERE $condition";
$result=$this->conn->query($sql);
if($result->num_rows>0){
return $result->fetch_assoc();
}
else{
return false;
}
}
public function Insert($table,$cols){
$sql="INSERT INTO $table SET $cols";
// echo $sql;
$result=$this->conn->query($sql);
if($this->conn->affected_rows>0){
return true;
}
else{
return false;
}
}
public function Update($table,$update_cols,$condition){
$sql="UPDATE $table SET $update_cols WHERE $condition";
$result=$this->conn->query($sql);
if($this->conn->affected_rows>0){
return true;
}
else{
return false;
}
}
public function Delete($table,$condition){
$sql="DELETE FROM $table WHERE $condition";
//echo $sql;
$result=$this->conn->query($sql);
if($this->conn->affected_rows>0){
return true;
}
else{
return false;
}
}
private function testPrivate(){
echo "I'm from Private";
}
protected function testProtected(){
echo "I'm from Protected";
}
public function Login($table,$cols,$condition){
$sql="SELECT $cols FROM $table WHERE $condition";
//echo $sql;
$result=$this->conn->query($sql);
if($result->num_rows==1){
return $result->fetch_assoc();
}
else{
return false;
}
}
public function readMore($content,$start,$end,$id,$btn_text){
$new_content=explode(" ",$content);
$to_array=array_slice($new_content,0,50);
$read_more= implode(" ",$to_array)." "."<a href=\"index.php?readmore=$id\">$btn_text</a>";
return $read_more;
}
}
$x=new Database("localhost","root","","cms");
//print_r($x->Login("users","*","email='[email protected]' AND password='7c4a8d09ca3762af61e59520943dc26494f8941b'"));
//$x->a="GoodGood";
//$x->testPublic(4);
/*if($x->getAll("students","name,mobile")!=false){
echo "<pre>";
print_r($x->getAll("students","*"));
echo "</pre>";
}*/
/*if($x->getById("students","*","id=1")!=false){
echo "<pre>";
print_r($x->getById("students","*","id=1"));
echo "</pre>";
}*/
/*if($x->getAllByIds("students","*","id in (1,3)")!=false){
echo "<pre>";
print_r($x->getAllByIds("students","*","id in (1,3)"));
echo "</pre>";
}*/
//echo $x->getAllwithTable("students","*");
/*echo $x->Insert("students","name='Mahbub',mobile='01988330022',address='Dhaka,Bangladesh'")?"Insert Success":"Insert Fail!";*/
/*echo $x->Update("students","name='Rokon',mobile='01988330044',address='Gazipur,Bangladesh'","id=5")?"Update Success":"Update Fail!";*/
//echo $x->Delete("students","id=2")?"Delete Success":"Delete Fail!";