-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_layer.php
executable file
·156 lines (111 loc) · 3.08 KB
/
db_layer.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
<?php
class DB {
function DB($host,$user,$pass,$database,$link = null) {
$this->host = $host;
$this->db = $database;
$this->user = $user;
$this->pass = $pass;
$this->last_query = '';
$this->last_error = '';
if($link != null) $this->link = $link;
else {
$this->link = @mysql_connect($this->host, $this->user, $this->pass);
if(!$this->link) {
die('Can\'t connect db!');
}
if(!@mysql_select_db($this->db, $this->link)){
die('Can\'t select db');
}
@mysql_query('set names utf8', $this->link);
@mysql_query('set character set utf8', $this->link);
}
return $this->link;
}
function query($query='')
{
if (!$query) return false;
else {
@mysql_select_db($this->db, $this->link);
@mysql_query('set names utf8', $this->link);
@mysql_query('set character set utf8', $this->link);
$this->last_query = $query;
$result = mysql_query($query, $this->link);
if(!$result) {
$this->last_error = mysql_error($this->link);
}
else {
$this->last_error = '';
}
return $result;
}
}
// returns an array of records
function fetchArray($query='', $key='')
{
// key determines which query result column to use as key
if ($result = $this->query($query)) {
if (mysql_num_rows($result) > 0) {
while ($arr = mysql_fetch_assoc($result)) {
if($key != '') {
$key_name = $arr[$key];
unset($arr[$key]);
$rows[$key_name] = $arr;
}
else {
$rows[] = $arr;
}
}
return $rows;
}
else return 0;
}
return false;
}
// returns a single record
function fetchRow($query='')
{
if ($row = $this->query($query)) {
if (mysql_num_rows($row) > 0) {
return mysql_fetch_assoc($row);
}
else return 0;
}
return false;
}
// returns last id from INSERT query
function insertId() {
return mysql_insert_id($this->link);
}
}
function is_num($int) {
if (preg_match("/^([0-9]+)$/", $int)) { return true; }
else { return false; }
}
// removes magic quotes added by php
function removeMagicQuotes($post)
{
if (get_magic_quotes_gpc()) {
if (is_array($post)) {
return array_map('stripslashes',$post);
}
else {
return stripslashes($post);
}
} else {
return $post; // magic quotes are not ON so we do nothing
}
}
// escapes and quotes string for sql if necessary
function escq($str) {
if (phpversion() >= '4.3.0') {
$str = mysql_real_escape_string($str);
} else {
$str = mysql_escape_string($str);
}
if (!is_numeric($str)) {
$str = "'" . $str . "'";
}
return $str;
}
define('NEW_LINE', "\r\n");
?>