This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
executable file
·92 lines (70 loc) · 2.4 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
<?php
class DBConnection extends Mysqli {
function __construct($host, $username, $password, $database) {
try{
parent::__construct($host, $username, $password, $database);
$this->set_charset("utf8");
if ($this->connect_error) {
throw new exception('Database Connection Error: '.$this->connect_error);
}
} catch(Exception $e) {
echo $e->getMessage();
}
}
function refValues($arr) {
$refs = array();
foreach ($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
function pquery($query, $params = NULL, $close = TRUE) {
if ($stmt = $this->prepare($query)) {
if ($params != NULL)
call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
$stmt->execute();
if ($close) {
$affected = $stmt->affected_rows;
$stmt->close();
return $affected;
} else {
return $stmt;
}
} else die('DBConnection: fetch failed: '.$this->error);
}
function fetch($query, $params = NULL, $class_name = "stdClass") {
$stmt = $this->pquery($query, $params, FALSE);
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));
$results = array();
while ($stmt->fetch()) {
$x = new $class_name;
foreach ($row as $key => $val) {
$x->$key = $val;
}
$results[] = $x;
}
$stmt->close();
return $results;
}
function fetch_one($query, $params = NULL,$class_name="stdClass") {
$stmt = $this->pquery($query, $params, FALSE);
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));
$result = null;
if ($stmt->fetch()) {
$result = new $class_name;
foreach ($row as $key => $val) {
$result->$key = $val;
}
}
$stmt->close();
return $result;
}
}
?>