@@ -19,543 +19,543 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Db extends Generic implements Db_Interface |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - public $type = 'mysqli'; |
|
| 26 | - public $statement; |
|
| 27 | - public $statement_query; |
|
| 28 | - public $statement_vars; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * alias function of select_db, changes the database we are working with. |
|
| 32 | - * |
|
| 33 | - * @param string $database the name of the database to use |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function useDb($database) |
|
| 37 | - { |
|
| 38 | - $this->selectDb($database); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * changes the database we are working with. |
|
| 43 | - * |
|
| 44 | - * @param string $database the name of the database to use |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function selectDb($database) |
|
| 48 | - { |
|
| 49 | - $this->connect(); |
|
| 50 | - mysqli_select_db($this->linkId, $database); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /* public: connection management */ |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Db::connect() |
|
| 57 | - * @param string $database |
|
| 58 | - * @param string $host |
|
| 59 | - * @param string $user |
|
| 60 | - * @param string $password |
|
| 61 | - * @return int|\mysqli |
|
| 62 | - */ |
|
| 63 | - public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
| 64 | - { |
|
| 65 | - /* Handle defaults */ |
|
| 66 | - if ($database == '') { |
|
| 67 | - $database = $this->database; |
|
| 68 | - } |
|
| 69 | - if ($host == '') { |
|
| 70 | - $host = $this->host; |
|
| 71 | - } |
|
| 72 | - if ($user == '') { |
|
| 73 | - $user = $this->user; |
|
| 74 | - } |
|
| 75 | - if ($password == '') { |
|
| 76 | - $password = $this->password; |
|
| 77 | - } |
|
| 78 | - if ($port == '') { |
|
| 79 | - $port = $this->port; |
|
| 80 | - } |
|
| 81 | - /* establish connection, select database */ |
|
| 82 | - if (!is_object($this->linkId)) { |
|
| 83 | - $this->connectionAttempt++; |
|
| 84 | - if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
| 85 | - error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
| 86 | - exit; |
|
| 87 | - } |
|
| 88 | - if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
| 89 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 90 | - return 0; |
|
| 91 | - } |
|
| 92 | - //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
| 93 | - $this->linkId = mysqli_init(); |
|
| 94 | - $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
| 95 | - if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
| 96 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 97 | - return 0; |
|
| 98 | - } |
|
| 99 | - $this->linkId->set_charset($this->characterSet); |
|
| 100 | - if ($this->linkId->connect_errno) { |
|
| 101 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 102 | - return 0; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - return $this->linkId; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Db::disconnect() |
|
| 110 | - * @return bool |
|
| 111 | - */ |
|
| 112 | - public function disconnect() |
|
| 113 | - { |
|
| 114 | - $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
| 115 | - $this->linkId = 0; |
|
| 116 | - return $return; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @param $string |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function real_escape($string = '') |
|
| 124 | - { |
|
| 125 | - if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
| 126 | - return $this->escape($string); |
|
| 127 | - } |
|
| 128 | - return mysqli_real_escape_string($this->linkId, $string); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * discard the query result |
|
| 133 | - * @return void |
|
| 134 | - */ |
|
| 135 | - public function free() |
|
| 136 | - { |
|
| 137 | - if (is_resource($this->queryId)) { |
|
| 138 | - @mysqli_free_result($this->queryId); |
|
| 139 | - } |
|
| 140 | - $this->queryId = 0; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Db::queryReturn() |
|
| 145 | - * |
|
| 146 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
| 147 | - * any rows and returns the row or rows immediately or FALSE on error |
|
| 148 | - * |
|
| 149 | - * @param mixed $query SQL Query to be used |
|
| 150 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
| 151 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
| 152 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
| 153 | - */ |
|
| 154 | - public function queryReturn($query, $line = '', $file = '') |
|
| 155 | - { |
|
| 156 | - $this->query($query, $line, $file); |
|
| 157 | - if ($this->num_rows() == 0) { |
|
| 158 | - return false; |
|
| 159 | - } elseif ($this->num_rows() == 1) { |
|
| 160 | - $this->next_record(MYSQLI_ASSOC); |
|
| 161 | - return $this->Record; |
|
| 162 | - } else { |
|
| 163 | - $out = []; |
|
| 164 | - while ($this->next_record(MYSQLI_ASSOC)) { |
|
| 165 | - $out[] = $this->Record; |
|
| 166 | - } |
|
| 167 | - return $out; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * db:qr() |
|
| 173 | - * |
|
| 174 | - * alias of queryReturn() |
|
| 175 | - * |
|
| 176 | - * @param mixed $query SQL Query to be used |
|
| 177 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
| 178 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
| 179 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
| 180 | - */ |
|
| 181 | - public function qr($query, $line = '', $file = '') |
|
| 182 | - { |
|
| 183 | - return $this->queryReturn($query, $line, $file); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * creates a prepaired statement from query |
|
| 188 | - * |
|
| 189 | - * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
| 190 | - * @return false|mysqli_stmt |
|
| 191 | - * @param string $line |
|
| 192 | - * @param string $file |
|
| 193 | - */ |
|
| 194 | - public function prepare($query, $line = '', $file = '') |
|
| 195 | - { |
|
| 196 | - if (!$this->connect()) { |
|
| 197 | - return 0; |
|
| 198 | - } |
|
| 199 | - $haltPrev = $this->haltOnError; |
|
| 200 | - $this->haltOnError = 'no'; |
|
| 201 | - $start = microtime(true); |
|
| 202 | - $this->statement = mysqli_prepare($this->linkId, $query); |
|
| 203 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 204 | - $this->statement_query = $query; |
|
| 205 | - $this->addLog($query, microtime(true) - $start, $line, $file); |
|
| 206 | - } |
|
| 207 | - return $this->statement; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Binds variables to a prepared statement as parameters |
|
| 212 | - * |
|
| 213 | - * @param string $types A string that contains one or more characters which specify the types for the corresponding bind variables: (i)nt, (d)ecimal, (s)tring, (b)inary |
|
| 214 | - * @param mixed $vars |
|
| 215 | - * @return bool indicates success |
|
| 216 | - */ |
|
| 217 | - public function bind_param($types, &...$vars) { |
|
| 218 | - $params = [$this->statement, $types]; |
|
| 219 | - foreach ($vars as &$var) { |
|
| 220 | - $params[] = &$var; // Ensure they stay references |
|
| 221 | - } |
|
| 222 | - $this->statement_vars &= $vars; |
|
| 223 | - return call_user_func_array('mysqli_stmt_bind_param', $params); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Executes a prepared statement |
|
| 228 | - * |
|
| 229 | - * @return bool success |
|
| 230 | - */ |
|
| 231 | - public function execute($line = '', $file = '') { |
|
| 232 | - $start = microtime(true); |
|
| 233 | - $return = mysqli_stmt_execute($this->statement); |
|
| 234 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 235 | - $vars = $this->statement_vars; |
|
| 236 | - $query = $result = preg_replace_callback('/\?/', function () use (&$vars) { |
|
| 237 | - return array_shift($vars); |
|
| 238 | - }, $this->statement_query); |
|
| 239 | - $this->addLog($query, microtime(true) - $start, $line, $file); |
|
| 240 | - } |
|
| 241 | - return $return; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Db::query() |
|
| 246 | - * |
|
| 247 | - * Sends an SQL query to the database |
|
| 248 | - * |
|
| 249 | - * @param mixed $queryString |
|
| 250 | - * @param int $line |
|
| 251 | - * @param string $file |
|
| 252 | - * @param bool $log |
|
| 253 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
| 254 | - */ |
|
| 255 | - public function query($queryString, $line = '', $file = '', $log = false) |
|
| 256 | - { |
|
| 257 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
| 258 | - /* The empty query string is passed on from the constructor, |
|
| 22 | + /** |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + public $type = 'mysqli'; |
|
| 26 | + public $statement; |
|
| 27 | + public $statement_query; |
|
| 28 | + public $statement_vars; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * alias function of select_db, changes the database we are working with. |
|
| 32 | + * |
|
| 33 | + * @param string $database the name of the database to use |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function useDb($database) |
|
| 37 | + { |
|
| 38 | + $this->selectDb($database); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * changes the database we are working with. |
|
| 43 | + * |
|
| 44 | + * @param string $database the name of the database to use |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function selectDb($database) |
|
| 48 | + { |
|
| 49 | + $this->connect(); |
|
| 50 | + mysqli_select_db($this->linkId, $database); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /* public: connection management */ |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Db::connect() |
|
| 57 | + * @param string $database |
|
| 58 | + * @param string $host |
|
| 59 | + * @param string $user |
|
| 60 | + * @param string $password |
|
| 61 | + * @return int|\mysqli |
|
| 62 | + */ |
|
| 63 | + public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
| 64 | + { |
|
| 65 | + /* Handle defaults */ |
|
| 66 | + if ($database == '') { |
|
| 67 | + $database = $this->database; |
|
| 68 | + } |
|
| 69 | + if ($host == '') { |
|
| 70 | + $host = $this->host; |
|
| 71 | + } |
|
| 72 | + if ($user == '') { |
|
| 73 | + $user = $this->user; |
|
| 74 | + } |
|
| 75 | + if ($password == '') { |
|
| 76 | + $password = $this->password; |
|
| 77 | + } |
|
| 78 | + if ($port == '') { |
|
| 79 | + $port = $this->port; |
|
| 80 | + } |
|
| 81 | + /* establish connection, select database */ |
|
| 82 | + if (!is_object($this->linkId)) { |
|
| 83 | + $this->connectionAttempt++; |
|
| 84 | + if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
| 85 | + error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
| 86 | + exit; |
|
| 87 | + } |
|
| 88 | + if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
| 89 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 90 | + return 0; |
|
| 91 | + } |
|
| 92 | + //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
| 93 | + $this->linkId = mysqli_init(); |
|
| 94 | + $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
| 95 | + if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
| 96 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 97 | + return 0; |
|
| 98 | + } |
|
| 99 | + $this->linkId->set_charset($this->characterSet); |
|
| 100 | + if ($this->linkId->connect_errno) { |
|
| 101 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 102 | + return 0; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + return $this->linkId; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Db::disconnect() |
|
| 110 | + * @return bool |
|
| 111 | + */ |
|
| 112 | + public function disconnect() |
|
| 113 | + { |
|
| 114 | + $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
| 115 | + $this->linkId = 0; |
|
| 116 | + return $return; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @param $string |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function real_escape($string = '') |
|
| 124 | + { |
|
| 125 | + if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
| 126 | + return $this->escape($string); |
|
| 127 | + } |
|
| 128 | + return mysqli_real_escape_string($this->linkId, $string); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * discard the query result |
|
| 133 | + * @return void |
|
| 134 | + */ |
|
| 135 | + public function free() |
|
| 136 | + { |
|
| 137 | + if (is_resource($this->queryId)) { |
|
| 138 | + @mysqli_free_result($this->queryId); |
|
| 139 | + } |
|
| 140 | + $this->queryId = 0; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Db::queryReturn() |
|
| 145 | + * |
|
| 146 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
| 147 | + * any rows and returns the row or rows immediately or FALSE on error |
|
| 148 | + * |
|
| 149 | + * @param mixed $query SQL Query to be used |
|
| 150 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
| 151 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
| 152 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
| 153 | + */ |
|
| 154 | + public function queryReturn($query, $line = '', $file = '') |
|
| 155 | + { |
|
| 156 | + $this->query($query, $line, $file); |
|
| 157 | + if ($this->num_rows() == 0) { |
|
| 158 | + return false; |
|
| 159 | + } elseif ($this->num_rows() == 1) { |
|
| 160 | + $this->next_record(MYSQLI_ASSOC); |
|
| 161 | + return $this->Record; |
|
| 162 | + } else { |
|
| 163 | + $out = []; |
|
| 164 | + while ($this->next_record(MYSQLI_ASSOC)) { |
|
| 165 | + $out[] = $this->Record; |
|
| 166 | + } |
|
| 167 | + return $out; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * db:qr() |
|
| 173 | + * |
|
| 174 | + * alias of queryReturn() |
|
| 175 | + * |
|
| 176 | + * @param mixed $query SQL Query to be used |
|
| 177 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
| 178 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
| 179 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
| 180 | + */ |
|
| 181 | + public function qr($query, $line = '', $file = '') |
|
| 182 | + { |
|
| 183 | + return $this->queryReturn($query, $line, $file); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * creates a prepaired statement from query |
|
| 188 | + * |
|
| 189 | + * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
| 190 | + * @return false|mysqli_stmt |
|
| 191 | + * @param string $line |
|
| 192 | + * @param string $file |
|
| 193 | + */ |
|
| 194 | + public function prepare($query, $line = '', $file = '') |
|
| 195 | + { |
|
| 196 | + if (!$this->connect()) { |
|
| 197 | + return 0; |
|
| 198 | + } |
|
| 199 | + $haltPrev = $this->haltOnError; |
|
| 200 | + $this->haltOnError = 'no'; |
|
| 201 | + $start = microtime(true); |
|
| 202 | + $this->statement = mysqli_prepare($this->linkId, $query); |
|
| 203 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 204 | + $this->statement_query = $query; |
|
| 205 | + $this->addLog($query, microtime(true) - $start, $line, $file); |
|
| 206 | + } |
|
| 207 | + return $this->statement; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Binds variables to a prepared statement as parameters |
|
| 212 | + * |
|
| 213 | + * @param string $types A string that contains one or more characters which specify the types for the corresponding bind variables: (i)nt, (d)ecimal, (s)tring, (b)inary |
|
| 214 | + * @param mixed $vars |
|
| 215 | + * @return bool indicates success |
|
| 216 | + */ |
|
| 217 | + public function bind_param($types, &...$vars) { |
|
| 218 | + $params = [$this->statement, $types]; |
|
| 219 | + foreach ($vars as &$var) { |
|
| 220 | + $params[] = &$var; // Ensure they stay references |
|
| 221 | + } |
|
| 222 | + $this->statement_vars &= $vars; |
|
| 223 | + return call_user_func_array('mysqli_stmt_bind_param', $params); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Executes a prepared statement |
|
| 228 | + * |
|
| 229 | + * @return bool success |
|
| 230 | + */ |
|
| 231 | + public function execute($line = '', $file = '') { |
|
| 232 | + $start = microtime(true); |
|
| 233 | + $return = mysqli_stmt_execute($this->statement); |
|
| 234 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 235 | + $vars = $this->statement_vars; |
|
| 236 | + $query = $result = preg_replace_callback('/\?/', function () use (&$vars) { |
|
| 237 | + return array_shift($vars); |
|
| 238 | + }, $this->statement_query); |
|
| 239 | + $this->addLog($query, microtime(true) - $start, $line, $file); |
|
| 240 | + } |
|
| 241 | + return $return; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Db::query() |
|
| 246 | + * |
|
| 247 | + * Sends an SQL query to the database |
|
| 248 | + * |
|
| 249 | + * @param mixed $queryString |
|
| 250 | + * @param int $line |
|
| 251 | + * @param string $file |
|
| 252 | + * @param bool $log |
|
| 253 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
| 254 | + */ |
|
| 255 | + public function query($queryString, $line = '', $file = '', $log = false) |
|
| 256 | + { |
|
| 257 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
| 258 | + /* The empty query string is passed on from the constructor, |
|
| 259 | 259 | * when calling the class without a query, e.g. in situations |
| 260 | 260 | * like these: '$db = new db_Subclass;' |
| 261 | 261 | */ |
| 262 | - if ($queryString == '') { |
|
| 263 | - return 0; |
|
| 264 | - } |
|
| 265 | - if (!$this->connect()) { |
|
| 266 | - return 0; |
|
| 267 | - /* we already complained in connect() about that. */ |
|
| 268 | - } |
|
| 269 | - $haltPrev = $this->haltOnError; |
|
| 270 | - $this->haltOnError = 'no'; |
|
| 271 | - // New query, discard previous result. |
|
| 272 | - if (is_resource($this->queryId)) { |
|
| 273 | - $this->free(); |
|
| 274 | - } |
|
| 275 | - if ($this->Debug) { |
|
| 276 | - printf("Debug: query = %s<br>\n", $queryString); |
|
| 277 | - } |
|
| 278 | - if ($log === true || (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false)) { |
|
| 279 | - $this->log($queryString, $line, $file); |
|
| 280 | - } |
|
| 281 | - $tries = 2; |
|
| 282 | - $try = 0; |
|
| 283 | - $this->queryId = false; |
|
| 284 | - while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
| 285 | - $try++; |
|
| 286 | - if ($try > 1) { |
|
| 287 | - @mysqli_close($this->linkId); |
|
| 288 | - $this->linkId = 0; |
|
| 289 | - } |
|
| 290 | - $start = microtime(true); |
|
| 291 | - $onlyRollback = true; |
|
| 292 | - $fails = -1; |
|
| 293 | - while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
| 294 | - $this->connect(); |
|
| 295 | - $fails++; |
|
| 296 | - try { |
|
| 297 | - $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
| 298 | - if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
| 299 | - //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
| 300 | - usleep(250000); // 0.25 second |
|
| 301 | - } else { |
|
| 302 | - $onlyRollback = false; |
|
| 303 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 304 | - $tries = 0; |
|
| 305 | - } |
|
| 306 | - break; |
|
| 307 | - } |
|
| 308 | - } catch (\mysqli_sql_exception $e) { |
|
| 309 | - if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
| 310 | - //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
| 311 | - usleep(250000); // 0.25 second |
|
| 312 | - } else { |
|
| 313 | - error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
| 314 | - $onlyRollback = false; |
|
| 315 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 316 | - $tries = 0; |
|
| 317 | - } |
|
| 318 | - break; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 323 | - $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
| 324 | - } |
|
| 325 | - $this->Row = 0; |
|
| 326 | - $this->Errno = @mysqli_errno($this->linkId); |
|
| 327 | - $this->Error = @mysqli_error($this->linkId); |
|
| 328 | - if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
| 329 | - //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - $this->haltOnError = $haltPrev; |
|
| 333 | - if ($onlyRollback === true && false === $this->queryId) { |
|
| 334 | - error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
| 335 | - } |
|
| 336 | - if (null === $this->queryId || $this->queryId === false) { |
|
| 337 | - $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
| 338 | - $this->halt('', $line, $file); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - // Will return nada if it fails. That's fine. |
|
| 342 | - return $this->queryId; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * @return array|null|object |
|
| 347 | - */ |
|
| 348 | - public function fetchObject() |
|
| 349 | - { |
|
| 350 | - $this->Record = @mysqli_fetch_object($this->queryId); |
|
| 351 | - return $this->Record; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /* public: walk result set */ |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Db::next_record() |
|
| 358 | - * |
|
| 359 | - * @param mixed $resultType |
|
| 360 | - * @return bool |
|
| 361 | - */ |
|
| 362 | - public function next_record($resultType = MYSQLI_BOTH) |
|
| 363 | - { |
|
| 364 | - if ($this->queryId === false) { |
|
| 365 | - $this->haltmsg('next_record called with no query pending.'); |
|
| 366 | - return 0; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
| 370 | - ++$this->Row; |
|
| 371 | - $this->Errno = mysqli_errno($this->linkId); |
|
| 372 | - $this->Error = mysqli_error($this->linkId); |
|
| 373 | - |
|
| 374 | - $stat = is_array($this->Record); |
|
| 375 | - if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
| 376 | - $this->free(); |
|
| 377 | - } |
|
| 378 | - return $stat; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * switch to position in result set |
|
| 383 | - * |
|
| 384 | - * @param integer $pos the row numbe starting at 0 to switch to |
|
| 385 | - * @return bool whetherit was successfu or not. |
|
| 386 | - */ |
|
| 387 | - public function seek($pos = 0) |
|
| 388 | - { |
|
| 389 | - $status = @mysqli_data_seek($this->queryId, $pos); |
|
| 390 | - if ($status) { |
|
| 391 | - $this->Row = $pos; |
|
| 392 | - } else { |
|
| 393 | - $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
| 394 | - /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
| 395 | - $rows = $this->num_rows(); |
|
| 396 | - @mysqli_data_seek($this->queryId, $rows); |
|
| 397 | - $this->Row = $rows; |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - return true; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Initiates a transaction |
|
| 405 | - * |
|
| 406 | - * @return bool |
|
| 407 | - */ |
|
| 408 | - public function transactionBegin() |
|
| 409 | - { |
|
| 410 | - if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
| 411 | - return true; |
|
| 412 | - } |
|
| 413 | - if (!$this->connect()) { |
|
| 414 | - return 0; |
|
| 415 | - } |
|
| 416 | - return mysqli_begin_transaction($this->linkId); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Commits a transaction |
|
| 421 | - * |
|
| 422 | - * @return bool |
|
| 423 | - */ |
|
| 424 | - public function transactionCommit() |
|
| 425 | - { |
|
| 426 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
| 427 | - return true; |
|
| 428 | - } |
|
| 429 | - return mysqli_commit($this->linkId); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Rolls back a transaction |
|
| 434 | - * |
|
| 435 | - * @return bool |
|
| 436 | - */ |
|
| 437 | - public function transactionAbort() |
|
| 438 | - { |
|
| 439 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
| 440 | - return true; |
|
| 441 | - } |
|
| 442 | - return mysqli_rollback($this->linkId); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
| 447 | - * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
| 448 | - * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
| 449 | - * |
|
| 450 | - * @param string $table |
|
| 451 | - * @param string $field |
|
| 452 | - * @return int|string |
|
| 453 | - */ |
|
| 454 | - public function getLastInsertId($table, $field) |
|
| 455 | - { |
|
| 456 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
| 457 | - return -1; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - return @mysqli_insert_id($this->linkId); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /* public: table locking */ |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Db::lock() |
|
| 467 | - * @param mixed $table |
|
| 468 | - * @param string $mode |
|
| 469 | - * @return bool|int|\mysqli_result |
|
| 470 | - */ |
|
| 471 | - public function lock($table, $mode = 'write') |
|
| 472 | - { |
|
| 473 | - $this->connect(); |
|
| 474 | - $query = 'lock tables '; |
|
| 475 | - if (is_array($table)) { |
|
| 476 | - foreach ($table as $key => $value) { |
|
| 477 | - if ($key == 'read' && $key != 0) { |
|
| 478 | - $query .= "$value read, "; |
|
| 479 | - } else { |
|
| 480 | - $query .= "$value $mode, "; |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - $query = mb_substr($query, 0, -2); |
|
| 484 | - } else { |
|
| 485 | - $query .= "$table $mode"; |
|
| 486 | - } |
|
| 487 | - $res = @mysqli_query($this->linkId, $query); |
|
| 488 | - if (!$res) { |
|
| 489 | - $this->halt("lock($table, $mode) failed."); |
|
| 490 | - return 0; |
|
| 491 | - } |
|
| 492 | - return $res; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Db::unlock() |
|
| 497 | - * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
| 498 | - * @return bool|int|\mysqli_result |
|
| 499 | - */ |
|
| 500 | - public function unlock($haltOnError = true) |
|
| 501 | - { |
|
| 502 | - $this->connect(); |
|
| 503 | - |
|
| 504 | - $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
| 505 | - if ($haltOnError === true && !$res) { |
|
| 506 | - $this->halt('unlock() failed.'); |
|
| 507 | - return 0; |
|
| 508 | - } |
|
| 509 | - return $res; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /* public: evaluate the result (size, width) */ |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Db::affectedRows() |
|
| 516 | - * @return int |
|
| 517 | - */ |
|
| 518 | - public function affectedRows() |
|
| 519 | - { |
|
| 520 | - return @mysqli_affected_rows($this->linkId); |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Db::num_rows() |
|
| 525 | - * @return int |
|
| 526 | - */ |
|
| 527 | - public function num_rows() |
|
| 528 | - { |
|
| 529 | - return @mysqli_num_rows($this->queryId); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Db::num_fields() |
|
| 534 | - * @return int |
|
| 535 | - */ |
|
| 536 | - public function num_fields() |
|
| 537 | - { |
|
| 538 | - return @mysqli_num_fields($this->queryId); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * gets an array of the table names in teh current datase |
|
| 543 | - * |
|
| 544 | - * @return array |
|
| 545 | - */ |
|
| 546 | - public function tableNames() |
|
| 547 | - { |
|
| 548 | - $return = []; |
|
| 549 | - $this->query('SHOW TABLES'); |
|
| 550 | - $i = 0; |
|
| 551 | - while ($info = $this->queryId->fetch_row()) { |
|
| 552 | - $return[$i]['table_name'] = $info[0]; |
|
| 553 | - $return[$i]['tablespace_name'] = $this->database; |
|
| 554 | - $return[$i]['database'] = $this->database; |
|
| 555 | - ++$i; |
|
| 556 | - } |
|
| 557 | - return $return; |
|
| 558 | - } |
|
| 262 | + if ($queryString == '') { |
|
| 263 | + return 0; |
|
| 264 | + } |
|
| 265 | + if (!$this->connect()) { |
|
| 266 | + return 0; |
|
| 267 | + /* we already complained in connect() about that. */ |
|
| 268 | + } |
|
| 269 | + $haltPrev = $this->haltOnError; |
|
| 270 | + $this->haltOnError = 'no'; |
|
| 271 | + // New query, discard previous result. |
|
| 272 | + if (is_resource($this->queryId)) { |
|
| 273 | + $this->free(); |
|
| 274 | + } |
|
| 275 | + if ($this->Debug) { |
|
| 276 | + printf("Debug: query = %s<br>\n", $queryString); |
|
| 277 | + } |
|
| 278 | + if ($log === true || (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false)) { |
|
| 279 | + $this->log($queryString, $line, $file); |
|
| 280 | + } |
|
| 281 | + $tries = 2; |
|
| 282 | + $try = 0; |
|
| 283 | + $this->queryId = false; |
|
| 284 | + while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
| 285 | + $try++; |
|
| 286 | + if ($try > 1) { |
|
| 287 | + @mysqli_close($this->linkId); |
|
| 288 | + $this->linkId = 0; |
|
| 289 | + } |
|
| 290 | + $start = microtime(true); |
|
| 291 | + $onlyRollback = true; |
|
| 292 | + $fails = -1; |
|
| 293 | + while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
| 294 | + $this->connect(); |
|
| 295 | + $fails++; |
|
| 296 | + try { |
|
| 297 | + $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
| 298 | + if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
| 299 | + //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
| 300 | + usleep(250000); // 0.25 second |
|
| 301 | + } else { |
|
| 302 | + $onlyRollback = false; |
|
| 303 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 304 | + $tries = 0; |
|
| 305 | + } |
|
| 306 | + break; |
|
| 307 | + } |
|
| 308 | + } catch (\mysqli_sql_exception $e) { |
|
| 309 | + if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
| 310 | + //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
| 311 | + usleep(250000); // 0.25 second |
|
| 312 | + } else { |
|
| 313 | + error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
| 314 | + $onlyRollback = false; |
|
| 315 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 316 | + $tries = 0; |
|
| 317 | + } |
|
| 318 | + break; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
| 323 | + $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
| 324 | + } |
|
| 325 | + $this->Row = 0; |
|
| 326 | + $this->Errno = @mysqli_errno($this->linkId); |
|
| 327 | + $this->Error = @mysqli_error($this->linkId); |
|
| 328 | + if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
| 329 | + //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + $this->haltOnError = $haltPrev; |
|
| 333 | + if ($onlyRollback === true && false === $this->queryId) { |
|
| 334 | + error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
| 335 | + } |
|
| 336 | + if (null === $this->queryId || $this->queryId === false) { |
|
| 337 | + $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
| 338 | + $this->halt('', $line, $file); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + // Will return nada if it fails. That's fine. |
|
| 342 | + return $this->queryId; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * @return array|null|object |
|
| 347 | + */ |
|
| 348 | + public function fetchObject() |
|
| 349 | + { |
|
| 350 | + $this->Record = @mysqli_fetch_object($this->queryId); |
|
| 351 | + return $this->Record; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /* public: walk result set */ |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Db::next_record() |
|
| 358 | + * |
|
| 359 | + * @param mixed $resultType |
|
| 360 | + * @return bool |
|
| 361 | + */ |
|
| 362 | + public function next_record($resultType = MYSQLI_BOTH) |
|
| 363 | + { |
|
| 364 | + if ($this->queryId === false) { |
|
| 365 | + $this->haltmsg('next_record called with no query pending.'); |
|
| 366 | + return 0; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
| 370 | + ++$this->Row; |
|
| 371 | + $this->Errno = mysqli_errno($this->linkId); |
|
| 372 | + $this->Error = mysqli_error($this->linkId); |
|
| 373 | + |
|
| 374 | + $stat = is_array($this->Record); |
|
| 375 | + if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
| 376 | + $this->free(); |
|
| 377 | + } |
|
| 378 | + return $stat; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * switch to position in result set |
|
| 383 | + * |
|
| 384 | + * @param integer $pos the row numbe starting at 0 to switch to |
|
| 385 | + * @return bool whetherit was successfu or not. |
|
| 386 | + */ |
|
| 387 | + public function seek($pos = 0) |
|
| 388 | + { |
|
| 389 | + $status = @mysqli_data_seek($this->queryId, $pos); |
|
| 390 | + if ($status) { |
|
| 391 | + $this->Row = $pos; |
|
| 392 | + } else { |
|
| 393 | + $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
| 394 | + /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
| 395 | + $rows = $this->num_rows(); |
|
| 396 | + @mysqli_data_seek($this->queryId, $rows); |
|
| 397 | + $this->Row = $rows; |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + return true; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Initiates a transaction |
|
| 405 | + * |
|
| 406 | + * @return bool |
|
| 407 | + */ |
|
| 408 | + public function transactionBegin() |
|
| 409 | + { |
|
| 410 | + if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
| 411 | + return true; |
|
| 412 | + } |
|
| 413 | + if (!$this->connect()) { |
|
| 414 | + return 0; |
|
| 415 | + } |
|
| 416 | + return mysqli_begin_transaction($this->linkId); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Commits a transaction |
|
| 421 | + * |
|
| 422 | + * @return bool |
|
| 423 | + */ |
|
| 424 | + public function transactionCommit() |
|
| 425 | + { |
|
| 426 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
| 427 | + return true; |
|
| 428 | + } |
|
| 429 | + return mysqli_commit($this->linkId); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Rolls back a transaction |
|
| 434 | + * |
|
| 435 | + * @return bool |
|
| 436 | + */ |
|
| 437 | + public function transactionAbort() |
|
| 438 | + { |
|
| 439 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
| 440 | + return true; |
|
| 441 | + } |
|
| 442 | + return mysqli_rollback($this->linkId); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
| 447 | + * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
| 448 | + * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
| 449 | + * |
|
| 450 | + * @param string $table |
|
| 451 | + * @param string $field |
|
| 452 | + * @return int|string |
|
| 453 | + */ |
|
| 454 | + public function getLastInsertId($table, $field) |
|
| 455 | + { |
|
| 456 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
| 457 | + return -1; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + return @mysqli_insert_id($this->linkId); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /* public: table locking */ |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Db::lock() |
|
| 467 | + * @param mixed $table |
|
| 468 | + * @param string $mode |
|
| 469 | + * @return bool|int|\mysqli_result |
|
| 470 | + */ |
|
| 471 | + public function lock($table, $mode = 'write') |
|
| 472 | + { |
|
| 473 | + $this->connect(); |
|
| 474 | + $query = 'lock tables '; |
|
| 475 | + if (is_array($table)) { |
|
| 476 | + foreach ($table as $key => $value) { |
|
| 477 | + if ($key == 'read' && $key != 0) { |
|
| 478 | + $query .= "$value read, "; |
|
| 479 | + } else { |
|
| 480 | + $query .= "$value $mode, "; |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + $query = mb_substr($query, 0, -2); |
|
| 484 | + } else { |
|
| 485 | + $query .= "$table $mode"; |
|
| 486 | + } |
|
| 487 | + $res = @mysqli_query($this->linkId, $query); |
|
| 488 | + if (!$res) { |
|
| 489 | + $this->halt("lock($table, $mode) failed."); |
|
| 490 | + return 0; |
|
| 491 | + } |
|
| 492 | + return $res; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Db::unlock() |
|
| 497 | + * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
| 498 | + * @return bool|int|\mysqli_result |
|
| 499 | + */ |
|
| 500 | + public function unlock($haltOnError = true) |
|
| 501 | + { |
|
| 502 | + $this->connect(); |
|
| 503 | + |
|
| 504 | + $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
| 505 | + if ($haltOnError === true && !$res) { |
|
| 506 | + $this->halt('unlock() failed.'); |
|
| 507 | + return 0; |
|
| 508 | + } |
|
| 509 | + return $res; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /* public: evaluate the result (size, width) */ |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Db::affectedRows() |
|
| 516 | + * @return int |
|
| 517 | + */ |
|
| 518 | + public function affectedRows() |
|
| 519 | + { |
|
| 520 | + return @mysqli_affected_rows($this->linkId); |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Db::num_rows() |
|
| 525 | + * @return int |
|
| 526 | + */ |
|
| 527 | + public function num_rows() |
|
| 528 | + { |
|
| 529 | + return @mysqli_num_rows($this->queryId); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Db::num_fields() |
|
| 534 | + * @return int |
|
| 535 | + */ |
|
| 536 | + public function num_fields() |
|
| 537 | + { |
|
| 538 | + return @mysqli_num_fields($this->queryId); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * gets an array of the table names in teh current datase |
|
| 543 | + * |
|
| 544 | + * @return array |
|
| 545 | + */ |
|
| 546 | + public function tableNames() |
|
| 547 | + { |
|
| 548 | + $return = []; |
|
| 549 | + $this->query('SHOW TABLES'); |
|
| 550 | + $i = 0; |
|
| 551 | + while ($info = $this->queryId->fetch_row()) { |
|
| 552 | + $return[$i]['table_name'] = $info[0]; |
|
| 553 | + $return[$i]['tablespace_name'] = $this->database; |
|
| 554 | + $return[$i]['database'] = $this->database; |
|
| 555 | + ++$i; |
|
| 556 | + } |
|
| 557 | + return $return; |
|
| 558 | + } |
|
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | /** |
@@ -86,19 +86,19 @@ discard block |
||
| 86 | 86 | exit; |
| 87 | 87 | } |
| 88 | 88 | if ($this->connectionAttempt >= $this->maxConnectErrors) { |
| 89 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 89 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset($this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 90 | 90 | return 0; |
| 91 | 91 | } |
| 92 | 92 | //error_log("real_connect($host, $user, $password, $database, $port)"); |
| 93 | 93 | $this->linkId = mysqli_init(); |
| 94 | 94 | $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
| 95 | 95 | if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
| 96 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 96 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset($this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 97 | 97 | return 0; |
| 98 | 98 | } |
| 99 | 99 | $this->linkId->set_charset($this->characterSet); |
| 100 | 100 | if ($this->linkId->connect_errno) { |
| 101 | - $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset( $this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 101 | + $this->halt("connect($host, $user, \$password) failed. ".(is_object($this->linkId) && isset($this->linkId->connect_error) ? $this->linkId->connect_error : '')); |
|
| 102 | 102 | return 0; |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | $return = mysqli_stmt_execute($this->statement); |
| 234 | 234 | if (!isset($GLOBALS['disable_db_queries'])) { |
| 235 | 235 | $vars = $this->statement_vars; |
| 236 | - $query = $result = preg_replace_callback('/\?/', function () use (&$vars) { |
|
| 236 | + $query = $result = preg_replace_callback('/\?/', function() use (&$vars) { |
|
| 237 | 237 | return array_shift($vars); |
| 238 | 238 | }, $this->statement_query); |
| 239 | 239 | $this->addLog($query, microtime(true) - $start, $line, $file); |
@@ -295,24 +295,24 @@ discard block |
||
| 295 | 295 | $fails++; |
| 296 | 296 | try { |
| 297 | 297 | $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
| 298 | - if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
| 298 | + if (in_array((int) @mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
| 299 | 299 | //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
| 300 | 300 | usleep(250000); // 0.25 second |
| 301 | 301 | } else { |
| 302 | 302 | $onlyRollback = false; |
| 303 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 303 | + if (in_array((int) @mysqli_errno($this->linkId), [1064])) { |
|
| 304 | 304 | $tries = 0; |
| 305 | 305 | } |
| 306 | 306 | break; |
| 307 | 307 | } |
| 308 | 308 | } catch (\mysqli_sql_exception $e) { |
| 309 | - if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
| 309 | + if (in_array((int) $e->getCode(), [1213, 2006, 3101, 1180])) { |
|
| 310 | 310 | //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
| 311 | 311 | usleep(250000); // 0.25 second |
| 312 | 312 | } else { |
| 313 | 313 | error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
| 314 | 314 | $onlyRollback = false; |
| 315 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
| 315 | + if (in_array((int) @mysqli_errno($this->linkId), [1064])) { |
|
| 316 | 316 | $tries = 0; |
| 317 | 317 | } |
| 318 | 318 | break; |
@@ -17,8 +17,7 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @access public |
| 19 | 19 | */ |
| 20 | -class Db extends Generic implements Db_Interface |
|
| 21 | -{ |
|
| 20 | +class Db extends Generic implements Db_Interface { |
|
| 22 | 21 | /** |
| 23 | 22 | * @var string |
| 24 | 23 | */ |
@@ -33,8 +32,7 @@ discard block |
||
| 33 | 32 | * @param string $database the name of the database to use |
| 34 | 33 | * @return void |
| 35 | 34 | */ |
| 36 | - public function useDb($database) |
|
| 37 | - { |
|
| 35 | + public function useDb($database) { |
|
| 38 | 36 | $this->selectDb($database); |
| 39 | 37 | } |
| 40 | 38 | |
@@ -44,8 +42,7 @@ discard block |
||
| 44 | 42 | * @param string $database the name of the database to use |
| 45 | 43 | * @return void |
| 46 | 44 | */ |
| 47 | - public function selectDb($database) |
|
| 48 | - { |
|
| 45 | + public function selectDb($database) { |
|
| 49 | 46 | $this->connect(); |
| 50 | 47 | mysqli_select_db($this->linkId, $database); |
| 51 | 48 | } |
@@ -60,8 +57,7 @@ discard block |
||
| 60 | 57 | * @param string $password |
| 61 | 58 | * @return int|\mysqli |
| 62 | 59 | */ |
| 63 | - public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
| 64 | - { |
|
| 60 | + public function connect($database = '', $host = '', $user = '', $password = '', $port = '') { |
|
| 65 | 61 | /* Handle defaults */ |
| 66 | 62 | if ($database == '') { |
| 67 | 63 | $database = $this->database; |
@@ -109,8 +105,7 @@ discard block |
||
| 109 | 105 | * Db::disconnect() |
| 110 | 106 | * @return bool |
| 111 | 107 | */ |
| 112 | - public function disconnect() |
|
| 113 | - { |
|
| 108 | + public function disconnect() { |
|
| 114 | 109 | $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
| 115 | 110 | $this->linkId = 0; |
| 116 | 111 | return $return; |
@@ -120,8 +115,7 @@ discard block |
||
| 120 | 115 | * @param $string |
| 121 | 116 | * @return string |
| 122 | 117 | */ |
| 123 | - public function real_escape($string = '') |
|
| 124 | - { |
|
| 118 | + public function real_escape($string = '') { |
|
| 125 | 119 | if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
| 126 | 120 | return $this->escape($string); |
| 127 | 121 | } |
@@ -132,8 +126,7 @@ discard block |
||
| 132 | 126 | * discard the query result |
| 133 | 127 | * @return void |
| 134 | 128 | */ |
| 135 | - public function free() |
|
| 136 | - { |
|
| 129 | + public function free() { |
|
| 137 | 130 | if (is_resource($this->queryId)) { |
| 138 | 131 | @mysqli_free_result($this->queryId); |
| 139 | 132 | } |
@@ -151,8 +144,7 @@ discard block |
||
| 151 | 144 | * @param string $file optionally pass __FILE__ calling the query for logging |
| 152 | 145 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
| 153 | 146 | */ |
| 154 | - public function queryReturn($query, $line = '', $file = '') |
|
| 155 | - { |
|
| 147 | + public function queryReturn($query, $line = '', $file = '') { |
|
| 156 | 148 | $this->query($query, $line, $file); |
| 157 | 149 | if ($this->num_rows() == 0) { |
| 158 | 150 | return false; |
@@ -178,8 +170,7 @@ discard block |
||
| 178 | 170 | * @param string $file optionally pass __FILE__ calling the query for logging |
| 179 | 171 | * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
| 180 | 172 | */ |
| 181 | - public function qr($query, $line = '', $file = '') |
|
| 182 | - { |
|
| 173 | + public function qr($query, $line = '', $file = '') { |
|
| 183 | 174 | return $this->queryReturn($query, $line, $file); |
| 184 | 175 | } |
| 185 | 176 | |
@@ -191,8 +182,7 @@ discard block |
||
| 191 | 182 | * @param string $line |
| 192 | 183 | * @param string $file |
| 193 | 184 | */ |
| 194 | - public function prepare($query, $line = '', $file = '') |
|
| 195 | - { |
|
| 185 | + public function prepare($query, $line = '', $file = '') { |
|
| 196 | 186 | if (!$this->connect()) { |
| 197 | 187 | return 0; |
| 198 | 188 | } |
@@ -252,8 +242,7 @@ discard block |
||
| 252 | 242 | * @param bool $log |
| 253 | 243 | * @return mixed 0 if no query or query id handler, safe to ignore this return |
| 254 | 244 | */ |
| 255 | - public function query($queryString, $line = '', $file = '', $log = false) |
|
| 256 | - { |
|
| 245 | + public function query($queryString, $line = '', $file = '', $log = false) { |
|
| 257 | 246 | /* No empty queries, please, since PHP4 chokes on them. */ |
| 258 | 247 | /* The empty query string is passed on from the constructor, |
| 259 | 248 | * when calling the class without a query, e.g. in situations |
@@ -345,8 +334,7 @@ discard block |
||
| 345 | 334 | /** |
| 346 | 335 | * @return array|null|object |
| 347 | 336 | */ |
| 348 | - public function fetchObject() |
|
| 349 | - { |
|
| 337 | + public function fetchObject() { |
|
| 350 | 338 | $this->Record = @mysqli_fetch_object($this->queryId); |
| 351 | 339 | return $this->Record; |
| 352 | 340 | } |
@@ -359,8 +347,7 @@ discard block |
||
| 359 | 347 | * @param mixed $resultType |
| 360 | 348 | * @return bool |
| 361 | 349 | */ |
| 362 | - public function next_record($resultType = MYSQLI_BOTH) |
|
| 363 | - { |
|
| 350 | + public function next_record($resultType = MYSQLI_BOTH) { |
|
| 364 | 351 | if ($this->queryId === false) { |
| 365 | 352 | $this->haltmsg('next_record called with no query pending.'); |
| 366 | 353 | return 0; |
@@ -384,8 +371,7 @@ discard block |
||
| 384 | 371 | * @param integer $pos the row numbe starting at 0 to switch to |
| 385 | 372 | * @return bool whetherit was successfu or not. |
| 386 | 373 | */ |
| 387 | - public function seek($pos = 0) |
|
| 388 | - { |
|
| 374 | + public function seek($pos = 0) { |
|
| 389 | 375 | $status = @mysqli_data_seek($this->queryId, $pos); |
| 390 | 376 | if ($status) { |
| 391 | 377 | $this->Row = $pos; |
@@ -405,8 +391,7 @@ discard block |
||
| 405 | 391 | * |
| 406 | 392 | * @return bool |
| 407 | 393 | */ |
| 408 | - public function transactionBegin() |
|
| 409 | - { |
|
| 394 | + public function transactionBegin() { |
|
| 410 | 395 | if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
| 411 | 396 | return true; |
| 412 | 397 | } |
@@ -421,8 +406,7 @@ discard block |
||
| 421 | 406 | * |
| 422 | 407 | * @return bool |
| 423 | 408 | */ |
| 424 | - public function transactionCommit() |
|
| 425 | - { |
|
| 409 | + public function transactionCommit() { |
|
| 426 | 410 | if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
| 427 | 411 | return true; |
| 428 | 412 | } |
@@ -434,8 +418,7 @@ discard block |
||
| 434 | 418 | * |
| 435 | 419 | * @return bool |
| 436 | 420 | */ |
| 437 | - public function transactionAbort() |
|
| 438 | - { |
|
| 421 | + public function transactionAbort() { |
|
| 439 | 422 | if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
| 440 | 423 | return true; |
| 441 | 424 | } |
@@ -451,8 +434,7 @@ discard block |
||
| 451 | 434 | * @param string $field |
| 452 | 435 | * @return int|string |
| 453 | 436 | */ |
| 454 | - public function getLastInsertId($table, $field) |
|
| 455 | - { |
|
| 437 | + public function getLastInsertId($table, $field) { |
|
| 456 | 438 | if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
| 457 | 439 | return -1; |
| 458 | 440 | } |
@@ -468,8 +450,7 @@ discard block |
||
| 468 | 450 | * @param string $mode |
| 469 | 451 | * @return bool|int|\mysqli_result |
| 470 | 452 | */ |
| 471 | - public function lock($table, $mode = 'write') |
|
| 472 | - { |
|
| 453 | + public function lock($table, $mode = 'write') { |
|
| 473 | 454 | $this->connect(); |
| 474 | 455 | $query = 'lock tables '; |
| 475 | 456 | if (is_array($table)) { |
@@ -497,8 +478,7 @@ discard block |
||
| 497 | 478 | * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
| 498 | 479 | * @return bool|int|\mysqli_result |
| 499 | 480 | */ |
| 500 | - public function unlock($haltOnError = true) |
|
| 501 | - { |
|
| 481 | + public function unlock($haltOnError = true) { |
|
| 502 | 482 | $this->connect(); |
| 503 | 483 | |
| 504 | 484 | $res = @mysqli_query($this->linkId, 'unlock tables'); |
@@ -515,8 +495,7 @@ discard block |
||
| 515 | 495 | * Db::affectedRows() |
| 516 | 496 | * @return int |
| 517 | 497 | */ |
| 518 | - public function affectedRows() |
|
| 519 | - { |
|
| 498 | + public function affectedRows() { |
|
| 520 | 499 | return @mysqli_affected_rows($this->linkId); |
| 521 | 500 | } |
| 522 | 501 | |
@@ -524,8 +503,7 @@ discard block |
||
| 524 | 503 | * Db::num_rows() |
| 525 | 504 | * @return int |
| 526 | 505 | */ |
| 527 | - public function num_rows() |
|
| 528 | - { |
|
| 506 | + public function num_rows() { |
|
| 529 | 507 | return @mysqli_num_rows($this->queryId); |
| 530 | 508 | } |
| 531 | 509 | |
@@ -533,8 +511,7 @@ discard block |
||
| 533 | 511 | * Db::num_fields() |
| 534 | 512 | * @return int |
| 535 | 513 | */ |
| 536 | - public function num_fields() |
|
| 537 | - { |
|
| 514 | + public function num_fields() { |
|
| 538 | 515 | return @mysqli_num_fields($this->queryId); |
| 539 | 516 | } |
| 540 | 517 | |
@@ -543,8 +520,7 @@ discard block |
||
| 543 | 520 | * |
| 544 | 521 | * @return array |
| 545 | 522 | */ |
| 546 | - public function tableNames() |
|
| 547 | - { |
|
| 523 | + public function tableNames() { |
|
| 548 | 524 | $return = []; |
| 549 | 525 | $this->query('SHOW TABLES'); |
| 550 | 526 | $i = 0; |