-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.php
148 lines (119 loc) · 4.24 KB
/
mysql.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
<?
require_once('database.php');
class Mysql extends Database {
static private $instance;
private $last_id;
protected function __construct($dbHost=null, $dbName=null, $dbUser=null, $dbPass=null) {
parent::__construct($dbHost, $dbName, $dbUser, $dbPass);
}
static function getInstance($dbHost, $dbName, $dbUser, $dbPass) {
if(!Mysql::$instance) {
Mysql::$instance = new Mysql($dbHost, $dbName, $dbUser, $dbPass);
}
return Mysql::$instance;
}
public function __set($name, $value) {
if (isset($name) && isset($value)) {
parent::__set($name, $value);
}
}
public function __get($name) {
if (isset($name)) {
return parent::__get($name);
}
}
public function connected() {
if (is_resource($this->connection)) {
return true;
} else {
return false;
}
}
public function affectedRows() {
return mysql_affected_rows($this->connection);
}
public function open() {
if (is_null($this->database))
die("MySQL database not selected");
if (is_null($this->hostname))
die("MySQL hostname not set");
$this->connection = @mysql_connect($this->hostname, $this->username, $this->password);
if ($this->connection === false)
die("Could not connect to database. Check your username and password then try again.\n");
if (!mysql_select_db($this->database, $this->connection)) {
die("Could not select database");
}
}
public function close() {
mysql_close($this->connection);
$this->connection = null;
}
public function query($sql) {
if ($this->connection === false) {
die('No Database Connection Found.');
}
$result = @mysql_query($sql,$this->connection);
if ($result === false) {
die(mysql_error());
}
return $result;
}
public function insert($table, $data)
{
global $lang, $admin_lang; if ($admin_lang) $lang = $admin_lang;
$args = array();
foreach($data as $key => $value)
{
$args[$key] = mysql_real_escape_string($value);
}
$keys = array_keys($data);
$values = array_values($args);
$query = "insert INTO {$table} (" . implode(", ", $keys) . ") VALUES ('" . implode("', '", $values) . "')";
$res = mysql_query($query);
$this->last_id = mysql_insert_id();
}
public function update($table, $data, $where)
{
global $lang, $admin_lang; if ($admin_lang) $lang = $admin_lang;
$args = array();
foreach($data as $key => $value)
{
$args[] = $key ." = '" .mysql_real_escape_string($value) . "'";
}
$keys = array_keys($where);
$where_sql = "WHERE {$keys[0]} = {$where[$keys[0]]}";
for($i = 1; $i < count($where); $i++)
{
$where_sql .= " AND {$keys[$i]} = {$where[$keys[$i]]}";
}
$query = "UPDATE {$table} SET " . implode(',',$args) . " {$where_sql} ";
$this->query($query);
}
public function delete($table, $id, $where)
{
global $lang, $admin_lang; if ($admin_lang) $lang = $admin_lang;
$keys = array_keys($where);
$where_sql = "WHERE {$key[0]} = {$where[$key[0]]}";
for($i = 1; $i < count($where); $i++)
{
$where_sql .= " AND {$key[$i]} = {$where[$key[$i]]}";
}
$query = "DELETE FROM {$table} WHERE {$key[0]} = {$where_sql} ";
$this->query($query);
}
public function fetchArray($result) {
if ($this->connection === false) {
die('No Database Connection Found.');
}
$data = @mysql_fetch_array($result);
if (!is_array($data)) {
die(mysql_error());
}
return $data;
}
public function getLastId()
{
return $this->last_id;
}
}
?>