Repository navigation
Expand file tree
/
Copy pathsql.php
More file actions
executable file
·269 lines (230 loc) · 9.53 KB
/
Copy pathsql.php
File metadata and controls
executable file
·269 lines (230 loc) · 9.53 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/*======================================================================*\
FDSQL Class
Author: Fernan de Dios
Email: info@fernandedios.com
Date: 09/25/2010
Version: 1.07
General Notes: $file = file executing the script, optional
$line = line number, optional
\*======================================================================*/
include_once("string.php");
class FDSQL extends FDUtilString {
var $db_link;
var $db_host;
var $db_name;
var $db_user;
var $db_pass;
/*======================================================================*\
Function: openDb
Purpose: open database connection
\*======================================================================*/
public function openDB($host, $name, $user, $pass, $file = __FILE__, $line = __LINE__){
$this->db_host = $host;
$this->db_user = $user;
$this->db_pass = $pass;
$this->db_name = $name;
if (!$this->db_link){
$this->db_link = mysql_connect($this->db_host, $this->db_user, $this->db_pass) OR $this->showError("Can't connect to database: " . mysql_error(), "die", $file, $line);
}
if (!mysql_select_db($this->db_name))
$this->showError("Can't select database: " . mysql_error(), "die", $file, $line);
}
/*======================================================================*\
Function: createDb
Purpose: create new database
Notes: $usedb = if set to true, it will select the newly created db
\*======================================================================*/
public function createDb($name_db, $use_db = false, $file = __FILE__, $line = __LINE__) {
$query = "CREATE DATABASE $name_db";
$this->runQuery($query, $file, $line);
if($use_db){
$this->db_name = $name_db;
if (!mysql_select_db($this->db_name)){
$this->showError("Can't select database: " . mysql_error(), "die", $file, $line);
}
}
}
/*======================================================================*\
Function: createTable
Purpose: create a table on the database
Notes: $primary (e.g. $primary["name"] = "id";
$primary["type"] = "int";
$primary["inc"] = "AUTO_INCREMENT";
$primary["null"] = "NOT NULL"; )
$fields (e.g. $fields["comments"] = "varchar(255)";
$fields["size"] = "int"; )
\*======================================================================*/
public function createTable($table_name, $primary, $fields){
$primary_field = $primary["name"] . " " . $primary["type"] . " " . $primary["null"] . " " . $primary["inc"] . ", PRIMARY KEY(" . $primary["name"] . ")";
$other_fields = array();
foreach ($fields as $key => $value) {
$other_fields[] = $key . " " . $value;
}
$com_fields = implode(",", $other_fields);
$query = "CREATE TABLE $table_name ($primary_field, $com_fields)";
$this->runQuery($query);
}
/*======================================================================*\
Function: getAll, getAllOrdered, getAllOrderedLimit, getField
Purpose: get records from db
Return: Array
Notes: $table = table name
$order = column reference for ordering
$dir = ASC or DESC
$offset = start getting records at this number
$count = number of records to get
$index = column reference
$id = actual index value
$prefs = multiple references.
(e.g. $prefs["username"] = "'fernan'";
$prefs["password"] = "'password'";
WHERE username = 'fernan' AND password = 'password'
important! enclose with '' if value is a string)
\*======================================================================*/
public function getAll($table){
$query = "SELECT * FROM {$table}";
$results = $this->runQuery($query);
return $results;
}
public function getAllOrdered($table, $order, $dir = "ASC"){
$query = "SELECT * FROM $table ORDER BY $order $dir";
$results = $this->runQuery($query);
return $results;
}
public function getAllOrderedLimit($table, $order, $dir = "ASC", $offset = 0, $count = 1) {
$query = "SELECT * FROM $table ORDER BY $order $dir LIMIT $offset, $count";
$results = $this->runQuery($query);
return $results;
}
public function getField($table, $field, $index, $id) {
$query = "SELECT $field FROM $table WHERE $index = $id";
$results = $this->runQuery($query);
return $results;
}
public function getFieldMultiRef($table, $field, $arr_pref){
$where = array();
foreach ($arr_pref as $key => $value) {
$where[] = $key . " = " . $value;
}
$and_where = implode(" AND ", $where);
$query = "SELECT $field FROM $table WHERE $and_where";
$results = $this->runQuery($query);
return $results;
}
public function getNumRows($table, $pref, $file = __FILE__, $line = __LINE__) {
$where = array();
foreach ($pref as $key => $value) {
$where[] = $key . " = " . $value;
}
$and_where = implode(" AND ", $where);
$query = "SELECT COUNT(*) as num FROM $table $and_where";
$this->runQuery($query, $file, $line);
}
/*======================================================================*\
Function: insertRow
Purpose: insert record to db
Return: id of the inserted record
Notes: $data should be an array
(e.g. $data["comments"] = "'hello there!'";
where the key is the field name
important! enclose with '' if value is a string )
\*======================================================================*/
public function insertRow($table, $data, $file = __FILE__, $line = __LINE__) {
$fields = array();
$values = array();
foreach($data as $key => $value){
$fields[] = $key;
$values[] = "'" . $value . "'";
}
$cm_fields = implode(',', $fields);
$cm_values = implode(',', $values);
$query = "INSERT into $table ($cm_fields) values ($cm_values)";
$this->runQuery($query, $file, $line);
return mysql_insert_id();
}
/*======================================================================*\
Function: updateRows
Purpose: update database records(s)
Notes: $index = column reference (e.g. id)
$id = actual index value (e.g. 1 )
$data = array of updated values, key is the field name
(e.g. $data["fname"] = "'Fernan'";
important! enclose with '' if value is a string)
$table = table name
\*======================================================================*/
public function updateRows($table, $data, $index, $id, $file = __FILE__, $line = __LINE__){
$setData = array();
foreach ($data as $key => $value) {
$setData[] = $key . "='" . $value . "'";
}
$upd = implode(',', $setData);
$query = "UPDATE $table SET $upd WHERE $index = $id";
$this->runQuery($query, $file, $line);
}
/*======================================================================*\
Function: deleteRows
Purpose: remove database records(s)
Notes: $index = column reference (e.g. id)
$id = actual index value (e.g. 1 )
$table = table name
\*======================================================================*/
public function deleteRows($table, $index, $id, $file = __FILE__, $line = __LINE__) {
$query = "DELETE FROM $table WHERE $index = $id";
$this->runQuery($query, $file, $line);
}
/*======================================================================*\
Function: runQuery
Purpose: main sql query method
\*======================================================================*/
public function runQuery($query, $file = __FILE__, $line = __LINE__){
$result = mysql_query($query) OR $this->showError(mysql_error() . "<br />SQL Query: " . $query, "die", $file, $line);
$total = ($this->getQueryType($query) == "SELECT") ? mysql_num_rows($result) : mysql_affected_rows();
if ($total) { return $result; } else { return 0; }
}
/*======================================================================*\
Function: getQueryType
Purpose: get query type from query string
\*======================================================================*/
public function getQueryType($query) {
$temp = explode(" ", $query);
return $temp[0];
}
/*======================================================================*\
Function: sanitizeSqlString
Purpose: prepare string for database entry
\*======================================================================*/
public function sanitizeSqlString($var) {
$var = trim($var);
$var = $this->removeComma($var, '-|-');
if(function_exists('mysql_real_escape_string')) {
return mysql_real_escape_string($var);
}
else {
return addslashes($var);
}
}
/*======================================================================*\
Function: removeComma
Purpose: used to prepare string for db entry
\*======================================================================*/
public function removeComma($var, $rep) {
return str_replace(",", $rep, $var);
}
/*======================================================================*\
Function: displayFromDb
Purpose: Format string coming from the database
\*======================================================================*/
public function displayFromDb($var) {
$var = str_replace("-|-", ",", $var);
return stripslashes($var);
}
/*======================================================================*\
Function: closeDB
Purpose: close database connection
\*======================================================================*/
public function closeDB(){
if ($this->db_link) mysql_close($this->db_link);
}
} //end class
?>