-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL.class.php
138 lines (123 loc) · 3.53 KB
/
MySQL.class.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
<?php
require_once(dirname(__FILE__)."/Exceptions.php");
class MySQL extends MySQLi{
var $m_server;
var $m_dbname;
function __construct($server,$dbname,$user,$pass = ""){
if(trim($server) && trim($dbname) && trim($user)){
parent::__construct($server, $user, $pass, $dbname);
$this->m_server = $server;
$this->m_dbname = $dbname;
}else{
throw new InvalidArgumentException("Parameters missing");
}
if($this->connect_errno){
throw new MySQLConnectionException($this->error);
}
}
function execute($query){
return $this->query($query);
}
function insertFromArray($table,$data,$replace =false){
if (trim($table) && is_array($data)){
$insertPair = Array();
while(list($key,$value)=each($data)){
$insertPair[] = $this->real_escape_string($key)."='".$this->real_escape_string($value)."'";
}
if(count($insertPair) > 0){
if($replace == true){
$query = ("REPLACE INTO ".$this->real_escape_string($table)." SET ".implode(",",$insertPair));
}else{
$query = ("INSERT INTO ".$this->real_escape_string($table)." SET ".implode(",",$insertPair));
}
#echo "$query\n";
$this->execute($query);
if($this->errno){
throw new MySQLInsertException($this->errno." - ".$this->error."\n\t$query");
}
return $this->insert_id;
}
}
return false;
}
function insert($table,$fields,$values){
if (trim($table) && is_array($fields) && is_array($values)){
echo "got here";
$insertPair = Array();
if(is_array($fields)){
while(list($key,$value)=each($fields)){
if(array_key_exists($key,$values)){
$insertPair[] = $this->real_escape_string($value)."='".$this->real_escape_string($values[$key])."'";
}
}
}
if(count($insertPair) > 0){
$query = ("INSERT INTO ".$this->real_escape_string($table)." SET ".implode(",",$insertPair));
//echo "query="."$query\n";
$this->execute($query);
if($this->errno){
throw new MySQLInsertException($this->errno." - ".$this->error."\n\t$query");
}
return $this->insert_id;
}
}
}
/* Returns first field of first row in resultset */
function getValue($query){
$result = $this->execute($query);
if($result != null){
if($result->num_rows > 0){
$field = $result->fetch_array(MYSQLI_NUM);
if(is_array($field)){
$result->close();
return $field[0];
}
}
$result->close();
}
return false;
}
/* Returns first row in resultset */
function getValues($query){
$result = $this->execute($query);
if($result->num_rows > 0){
$field = $result->fetch_array(MYSQLI_ASSOC);
if(is_array($field)){
$result->close();
return $field;
}
}
$result->close();
return false;
}
/* Returns first row in resultset */
function getRows($query){
$result = $this->execute($query);
$rows = array();
if($result != null && $result->num_rows > 0){
while($tmp = $result->fetch_array(MYSQLI_ASSOC)){
if(is_array($tmp)){
$rows[] = $tmp;
}
}
$result->close();
return $rows;
}
return false;
}
function delete($table,$condition){
if(is_array($condition)){
while(list($key,$value)=each($condition)){
$insertPair[] = $this->real_escape_string($key)."='".$this->real_escape_string($value)."'";
}
}else{
throw new MySQLDeleteException("No conditions were supplied to the delete method for table $table");
}
$query = "DELETE FROM ".$this->real_escape_string($table)." WHERE ".implode(" AND ",$insertPair);
$this->execute($query);
if($this->errno){
throw new MySQLDeleteException($this->errno." - ".$this->error."\n\t$query");
}
}
}
?>