-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
122 lines (101 loc) · 2.79 KB
/
db.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
<?
//requires conf.php
class db{
private $mysqli;
private $host, $user, $pass, $db;
public function __construct($db='essc', $host = '127.0.0.1', $user ='root', $pass='**password**') {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->connect();
}
public function connect(){
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db);
}
public function __wakeup()
{
$this->connect();
}
public function __destruct(){
$this->close();
}
public function close() {
if($this->mysqli){
try{
$this->mysqli->close();
}
catch(Exception $e){
}
$this->mysqli = null;
}
}
public function select_db($dbname) {
return $this->mysqli->select_db($dbname);
}
public function query($sql) {
return $this->mysqli->query($sql);
}
public function affected_rows() {
return $this->mysqli->affected_rows;
}
public function error() {
return $this->mysqli->error;
}
public function fetch_all($sql,$type=MYSQL_ASSOC) {
$res = $this->query($sql);
$arr = array();
if($res){
while($r = @$res->fetch_array($type)){
$arr[] = $r;
}
}
return $arr;
}
public function fetch_values($sql) {
$res = $this->query($sql);
$arr = array();
if($res){
while($r = $res->fetch_row()){
$arr[] = $r[0];
}
}
return $arr;
}
public function get($sql,$type=MYSQL_ASSOC) {
$res = $this->query($sql);
return $res ? $res->fetch_array($type) : array();
}
public function getv($sql) {
$res = $this->query($sql);
if($res){
$row = $res->fetch_array(MYSQL_NUM);
return $row[0];
}else{
return '';
}
}
public function getOpts($sql,$selected=NULL){
$res = $this->query($sql);
$opts = '';
while($row = $res->fetch_array(MYSQL_BOTH)){
$sel = $selected==$row['value']?'selected':'';
$opts .= "<option value='".(@$row['value']?$row['value']:$row[0])."' $sel>".(@$row['text']?$row['text']:$row[1])."</option>";
}
return $opts;
}
public function prepare($qry){
$stmt = $this->mysqli->stmt_init();
$stmt->prepare($qry);
return $stmt;
}
public function esc(&$val) {
if(is_array($val)){
array_walk($val, array($this, 'esc'));
}else{
if(get_magic_quotes_gpc()) $val = stripslashes($val);
$val = $this->mysqli->real_escape_string($val);
}
return $val;
}
}