1
|
<?php
|
2
|
class _database {
|
3
|
private $link = false;
|
4
|
private $result = false;
|
5
|
private $row = false;
|
6
|
|
7
|
public $settings = array(
|
8
|
"servername"=> "localhost",
|
9
|
"serverport"=> "3306",
|
10
|
"username" => false,
|
11
|
"password" => false,
|
12
|
"database" => false,
|
13
|
"persist" => false,
|
14
|
"dieonerror"=> false,
|
15
|
"showerror" => false,
|
16
|
"error_file"=> true
|
17
|
);
|
18
|
|
19
|
function __construct() {
|
20
|
global $db_config;
|
21
|
$this->settings = array_merge($this->settings, $db_config);
|
22
|
if($this->settings["error_file"] === true) $this->settings["error_file"] = dirname(__FILE__)."/__mysql_errors.log";
|
23
|
}
|
24
|
|
25
|
function connect() {
|
26
|
if (!$this->link) {
|
27
|
$this->link = ($this->settings["persist"]) ?
|
28
|
mysql_pconnect(
|
29
|
$this->settings["servername"].":".$this->settings["serverport"],
|
30
|
$this->settings["username"],
|
31
|
$this->settings["password"]
|
32
|
) :
|
33
|
mysql_connect(
|
34
|
$this->settings["servername"].":".$this->settings["serverport"],
|
35
|
$this->settings["username"],
|
36
|
$this->settings["password"]
|
37
|
) or $this->error();
|
38
|
}
|
39
|
if (!mysql_select_db($this->settings["database"], $this->link)) $this->error();
|
40
|
if($this->link) mysql_query("SET NAMES 'utf8'");
|
41
|
return ($this->link) ? true : false;
|
42
|
}
|
43
|
|
44
|
function query($sql) {
|
45
|
if (!$this->link && !$this->connect()) $this->error();
|
46
|
if (!($this->result = mysql_query($sql, $this->link))) $this->error($sql);
|
47
|
return ($this->result) ? true : false;
|
48
|
}
|
49
|
|
50
|
function nextr() {
|
51
|
if(!$this->result) {
|
52
|
$this->error("No query pending");
|
53
|
return false;
|
54
|
}
|
55
|
unset($this->row);
|
56
|
$this->row = mysql_fetch_array($this->result, MYSQL_BOTH);
|
57
|
return ($this->row) ? true : false ;
|
58
|
}
|
59
|
|
60
|
function get_row($mode = "both") {
|
61
|
if(!$this->row) return false;
|
62
|
|
63
|
$return = array();
|
64
|
switch($mode) {
|
65
|
case "assoc":
|
66
|
foreach($this->row as $k => $v) {
|
67
|
if(!is_int($k)) $return[$k] = $v;
|
68
|
}
|
69
|
break;
|
70
|
case "num":
|
71
|
foreach($this->row as $k => $v) {
|
72
|
if(is_int($k)) $return[$k] = $v;
|
73
|
}
|
74
|
break;
|
75
|
default:
|
76
|
$return = $this->row;
|
77
|
break;
|
78
|
}
|
79
|
return array_map("stripslashes",$return);
|
80
|
}
|
81
|
|
82
|
function get_all($mode = "both", $key = false) {
|
83
|
if(!$this->result) {
|
84
|
$this->error("No query pending");
|
85
|
return false;
|
86
|
}
|
87
|
$return = array();
|
88
|
while($this->nextr()) {
|
89
|
if($key !== false) $return[$this->f($key)] = $this->get_row($mode);
|
90
|
else $return[] = $this->get_row($mode);
|
91
|
}
|
92
|
return $return;
|
93
|
}
|
94
|
|
95
|
function f($index) {
|
96
|
return stripslashes($this->row[$index]);
|
97
|
}
|
98
|
|
99
|
function go_to($row) {
|
100
|
if(!$this->result) {
|
101
|
$this->error("No query pending");
|
102
|
return false;
|
103
|
}
|
104
|
if(!mysql_data_seek($this->result, $row)) $this->error();
|
105
|
}
|
106
|
|
107
|
function nf() {
|
108
|
if ($numb = mysql_num_rows($this->result) === false) $this->error();
|
109
|
return mysql_num_rows($this->result);
|
110
|
}
|
111
|
function af() {
|
112
|
return mysql_affected_rows();
|
113
|
}
|
114
|
function error($string="") {
|
115
|
$error = mysql_error();
|
116
|
if($this->settings["show_error"]) echo $error;
|
117
|
if($this->settings["error_file"] !== false) {
|
118
|
$handle = @fopen($this->settings["error_file"], "a+");
|
119
|
if($handle) {
|
120
|
@fwrite($handle, "[".date("Y-m-d H:i:s")."] ".$string." <".$error.">\n");
|
121
|
@fclose($handle);
|
122
|
}
|
123
|
}
|
124
|
if($this->settings["dieonerror"]) {
|
125
|
if(isset($this->result)) mysql_free_result($this->result);
|
126
|
mysql_close($this->link);
|
127
|
die();
|
128
|
}
|
129
|
}
|
130
|
function insert_id() {
|
131
|
if(!$this->link) return false;
|
132
|
return mysql_insert_id();
|
133
|
}
|
134
|
function escape($string){
|
135
|
if(!$this->link) return addslashes($string);
|
136
|
return mysql_real_escape_string($string);
|
137
|
}
|
138
|
|
139
|
function destroy(){
|
140
|
if (isset($this->result)) mysql_free_result($this->result);
|
141
|
if (isset($this->link)) mysql_close($this->link);
|
142
|
}
|
143
|
|
144
|
|
145
|
}
|
146
|
?>
|