1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DBAL; |
4
|
|
|
use mysqli; |
5
|
|
|
use mysqli_result; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* User: Gorlum |
9
|
|
|
* Date: 02.09.2015 |
10
|
|
|
* Time: 0:41 |
11
|
|
|
*/ |
12
|
|
|
class db_mysql_v5 { |
13
|
|
|
const DB_MYSQL_TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
14
|
|
|
const DB_MYSQL_TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
15
|
|
|
const DB_MYSQL_TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
16
|
|
|
const DB_MYSQL_TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Соединение с MySQL |
20
|
|
|
* |
21
|
|
|
* @var mysqli $link |
22
|
|
|
*/ |
23
|
|
|
public $link; |
24
|
|
|
/** |
25
|
|
|
* Статус соеднения с MySQL |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
public $connected = false; |
30
|
|
|
|
31
|
|
|
// public $dbsettings = array(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
public function mysql_connect($settings) { |
34
|
|
|
global $debug; |
|
|
|
|
35
|
|
|
|
36
|
|
|
static $need_keys = array('server', 'user', 'pass', 'name', 'prefix'); |
37
|
|
|
|
38
|
|
|
if ($this->connected) { |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (empty($settings) || !is_array($settings) || array_intersect($need_keys, array_keys($settings)) != $need_keys) { |
43
|
|
|
$debug->error_fatal('There is missconfiguration in your config.php. Check it again'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// @$this->link = mysql_connect($settings['server'], $settings['user'], $settings['pass']); |
|
|
|
|
47
|
|
|
// if(!is_resource($this->link)) { |
48
|
|
|
// $debug->error_fatal('DB Error - cannot connect to server', $this->mysql_error()); |
49
|
|
|
// } |
50
|
|
|
@$this->link = mysqli_connect($settings['server'], $settings['user'], $settings['pass'], $settings['name']); |
51
|
|
|
if (!is_object($this->link) || $this->link->connect_error) { |
52
|
|
|
$debug->error_fatal('DB Error - cannot connect to server error #' . $this->link->connect_errno, $this->link->connect_error); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
$this->mysql_query("/*!40101 SET NAMES 'utf8' */") |
57
|
|
|
or $debug->error_fatal('DB error - cannot set names 1 error #' . $this->link->errno, $this->link->error); |
|
|
|
|
58
|
|
|
$this->mysql_query("SET NAMES 'utf8';") |
59
|
|
|
or $debug->error_fatal('DB error - cannot set names 2 error #' . $this->link->errno, $this->link->error); |
|
|
|
|
60
|
|
|
|
61
|
|
|
//mysql_select_db($settings['name']) or $debug->error_fatal('DB error - cannot find DB on server', $this->mysql_error()); |
|
|
|
|
62
|
|
|
$this->mysql_query('SET SESSION TRANSACTION ISOLATION LEVEL ' . self::DB_MYSQL_TRANSACTION_SERIALIZABLE . ';') |
63
|
|
|
or $debug->error_fatal('DB error - cannot set desired isolation level error #' . $this->link->errno, $this->link->error); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->connected = true; |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function mysql_query($query_string) { |
71
|
|
|
return $this->link->query($query_string); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mysqli_result $query_result |
76
|
|
|
* |
77
|
|
|
* @return array|null |
78
|
|
|
*/ |
79
|
|
|
public function mysql_fetch_assoc(&$query_result) { |
80
|
|
|
return mysqli_fetch_assoc($query_result); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function mysql_fetch_row(&$query) { |
84
|
|
|
return mysqli_fetch_row($query); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function mysql_real_escape_string($unescaped_string) { |
88
|
|
|
return mysqli_real_escape_string($this->link, $unescaped_string); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function mysql_close_link() { |
92
|
|
|
if (is_object($this->link)) { |
93
|
|
|
$this->link->close(); |
94
|
|
|
$this->connected = false; |
95
|
|
|
unset($this->link); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function mysql_error() { |
102
|
|
|
return mysqli_error($this->link); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int|string |
107
|
|
|
*/ |
108
|
|
|
public function mysql_insert_id() { |
109
|
|
|
return mysqli_insert_id($this->link); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function mysql_num_rows(&$result) { |
113
|
|
|
return mysqli_num_rows($result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function mysql_affected_rows() { |
117
|
|
|
return mysqli_affected_rows($this->link); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function mysql_get_client_info() { |
121
|
|
|
return mysqli_get_client_info(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function mysql_get_server_info() { |
125
|
|
|
return mysqli_get_server_info($this->link); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function mysql_get_host_info() { |
129
|
|
|
return mysqli_get_host_info($this->link); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function mysql_stat() { |
133
|
|
|
return mysqli_stat($this->link); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.