forked from Tudor-B/SMA
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass.sma.database.php
executable file
·67 lines (67 loc) · 1.84 KB
/
class.sma.database.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
<?php
class SMA_Database {
private $config = array();
private $connected = false;
private $link = NULL;
private $statement = '';
private $result = NULL;
private $message = '';
public function __construct($config = array()) {
$this->config['host'] = 'localhost';
$this->config['user'] = 'root';
$this->config['pass'] = 'abcdurs1';
$this->config['data'] = 'sma';
$this->config['port'] = ini_get('mysqli.default_port');
$this->config['sock'] = ini_get('mysqli.default_socket');
if (is_array($config)) {
foreach ($config as $item => $value) {
if (array_key_exists($item, $this->config)) {
$this->config[$item] = $value;
}
}
}
$this->connect();
}
public function connect() {
if (!$this->connected) {
$this->link = new \mysqli($this->config['host'],$this->config['user'],$this->config['pass'],$this->config['data'],$this->config['port'],$this->config['sock']);
if (mysqli_connect_error()) {
throw new \Exception('Mysqli connection error: ' . $this->link->connect_error, $this->link->connect_errno);
} else {
$this->connected = true;
}
}
}
public function prepare($query, $params = array()) {
$params = array_map(array($this->link,'real_escape_string'), $params);
array_unshift($params, $query);
$query = call_user_func_array('sprintf', $params);
$this->statement = $query;
}
public function execute() {
if (empty($this->statement)) {
return false;
}
$this->result = $this->link->query($this->statement);
$this->statement = '';
return $this->fetch_records();
}
public function fetch_records() {
if (!$this->result) {
$this->message = 'No rows found';
return false;
}
if (is_object($this->result)) {
$r = array();
while ($res = $this->result->fetch_object()) {
$r[] = $res;
}
return $r;
} elseif ($this->result) {
return true;
} else {
return false;
}
}
}
?>