-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.php
More file actions
181 lines (180 loc) · 8.56 KB
/
DB.php
File metadata and controls
181 lines (180 loc) · 8.56 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
178
179
180
181
<?php
/*
* DB Class
* This class is used for database related (connect, get, insert, update, and delete) operations
* with PHP Data Objects (PDO)
* @author CodexWorld.com
* @url http://www.codexworld.com
* @license http://www.codexworld.com/license
*/
class DB {
// Database credentials
private $dbHost = 'localhost';
private $dbUsername = 'root';
private $dbPassword = '';
private $dbName = 'brazil1';
public $db;
/*
* Connect to the database and return db connecction
*/
public function __construct(){
if(!isset($this->db)){
// Connect to the database
try{
$conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName, $this->dbUsername, $this->dbPassword);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db = $conn;
}catch(PDOException $e){
die("Failed to connect with MySQL: " . $e->getMessage());
}
}
}
/*
* Returns rows from the database based on the conditions
* @param string name of the table
* @param array select, where, order_by, limit and return_type conditions
*/
// public function getRows($table,$conditions = array()){
public function getRows({
$sql = 'SELECT * FROM y2000';
// $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
// $sql .= ' FROM '.$table;
// if(array_key_exists("where",$conditions)){
// $sql .= ' WHERE ';
// $i = 0;
// foreach($conditions['where'] as $key => $value){
// $pre = ($i > 0)?' AND ':'';
// $sql .= $pre.$key." = '".$value."'";
// $i++;
// }
// }
// if(array_key_exists("order_by",$conditions)){
// $sql .= ' ORDER BY '.$conditions['order_by'];
// }
// if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
// $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
// }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
// $sql .= ' LIMIT '.$conditions['limit'];
// }
$query = $this->db->prepare($sql);
$query->execute();
// if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
// switch($conditions['return_type']){
// case 'count':
// $data = $query->rowCount();
// break;
// case 'single':
// $data = $query->fetch(PDO::FETCH_ASSOC);
// break;
// default:
// $data = '';
// }
// }else{
// if($query->rowCount() > 0){
$data = $query->fetchAll(PDO::FETCH_ASSOC);
// }
// }
return !empty($data)?$data:false;
}
/*
* Insert data into the database
* @param string name of the table
* @param array the data for inserting into the table
*/
// public function insert($table,$data){
// if(!empty($data) && is_array($data)){
// $columns = '';
// $values = '';
// $i = 0;
// if(!array_key_exists('created',$data)){
// $data['created'] = date("Y-m-d H:i:s");
// }
// if(!array_key_exists('modified',$data)){
// $data['modified'] = date("Y-m-d H:i:s");
// }
// $columnString = implode(',', array_keys($data));
// $valueString = ":".implode(',:', array_keys($data));
// $sql = "INSERT INTO ".$table." (".$columnString.") VALUES (".$valueString.")";
// $query = $this->db->prepare($sql);
// foreach($data as $key=>$val){
// $val = htmlspecialchars(strip_tags($val));
// $query->bindValue(':'.$key, $val);
// }
// $insert = $query->execute();
// if($insert){
// $data['id'] = $this->db->lastInsertId();
// return $data;
// }else{
// return false;
// }
// }else{
// return false;
// }
// }
//
// /*
// * Update data into the database
// * @param string name of the table
// * @param array the data for updating into the table
// * @param array where condition on updating data
// */
// public function update($table,$data,$conditions){
// if(!empty($data) && is_array($data)){
// $colvalSet = '';
// $whereSql = '';
// $i = 0;
// if(!array_key_exists('modified',$data)){
// $data['modified'] = date("Y-m-d H:i:s");
// }
// foreach($data as $key=>$val){
// $pre = ($i > 0)?', ':'';
// $val = htmlspecialchars(strip_tags($val));
// $colvalSet .= $pre.$key."='".$val."'";
// &nnbsp; $i++;
// }
// if(!empty($conditions)&& is_array($conditions)){
// $whereSql .= ' WHERE ';
// $i = 0;
// foreach($conditions as $key => $value){
// $pre = ($i > 0)?' AND ':'';
// $whereSql .= $pre.$key." = '".$value."'";
// $i++;
// }
// }
// $sql = "UPDATE ".$table." SET ".$colvalSet.$whereSql;
// $query = $this->db->prepare($sql);
// $update = $query->execute();
// return $update?$query->rowCount():false;
// }else{
// return false;
// }
// }
//
// /*
// * Delete data from the database
// * @param string name of the table
// * @param array where condition on deleting data
// */
// public function delete($table,$conditions){
// $whereSql = '';
// if(!empty($conditions)&& is_array($conditions)){
// $whereSql .= ' WHERE ';
// $i = 0;
// foreach($conditions as $key => $value){
// $pre = ($i > 0)?' AND ':'';
// $whereSql .= $pre.$key." = '".$value."'";
// $i++;
// }
// }
// $sql = "DELETE FROM ".$table.$whereSql;
// $delete = $this->db->exec($sql);
// return $delete?$delete:false;
// }
}