/** * Execute query * * @param string $query unparsed query * @param mixed ... unlimited number of variables for placeholders * @return mixed result set for "SELECT" statement / generated ID for an AUTO_INCREMENT field for insert statement / Affected rows count for DELETE/UPDATE statements */ public static function query($query) { if (!self::$raw) { fn_set_hook('db_query', $query); } $args = func_get_args(); $query = self::process($query, array_slice($args, 1), true); $result = false; if (!empty($query)) { if (!self::$raw) { fn_set_hook('db_query_process', $query); } if (defined('DEBUG_QUERIES')) { fn_print_r($query); } $time_start = microtime(true); $result = self::$_db->query($query); $a_row = self::$_db->affectedRows($result); Debugger::set_query($query, microtime(true) - $time_start); self::_error($result, $query); // Get last inserted ID $i_id = self::$_db->insertId(); if (!self::$raw) { fn_set_hook('db_query_executed', $query, $result); } // "true" will be returned for Update/Delete/Insert/Replace statements. "SELECT" returns MySQLi/PDO object if ($result === true) { $cmd = substr($query, 0, 6); // Check if it was insert statement with auto_increment value and return it if (!empty($i_id)) { $result = $i_id; } elseif ($cmd == 'UPDATE' || $cmd == 'DELETE' || $cmd == 'INSERT') { $result = $a_row; } // Check if query updated data in the database and run cache handlers if (!empty($result) && preg_match("/^(UPDATE|INSERT INTO|REPLACE INTO|DELETE FROM) " . self::$_table_prefix . "(\\w+) /", $query, $m)) { Registry::setChangedTables($m[2]); } } } self::$raw = false; return $result; }
/** * Execute query * * @param string $query unparsed query * @param mixed ... unlimited number of variables for placeholders * @return mixed result set for "SELECT" statement / generated ID for an AUTO_INCREMENT field for insert statement / Affected rows count for DELETE/UPDATE statements */ public static function query($query) { if (!self::$raw) { fn_set_hook('db_query', $query); } $args = func_get_args(); $query = self::process($query, array_slice($args, 1), true); $result = false; if (!empty($query)) { if (!self::$raw) { fn_set_hook('db_query_process', $query); } if (defined('DEBUG_QUERIES')) { fn_print_r($query); } $time_start = microtime(true); $result = self::$db->query($query); if (!self::error($result, $query)) { $insert_id = self::$db->insertId(); Debugger::set_query($query, microtime(true) - $time_start); if (!self::$raw) { fn_set_hook('db_query_executed', $query, $result); } // "true" will be returned for Update/Delete/Insert/Replace statements. "SELECT" returns MySQLi/PDO object if ($result === true) { $cmd = substr($query, 0, 6); // Check if it was insert statement with auto_increment value and return it if (!empty($insert_id)) { $result = $insert_id; } elseif ($cmd == 'UPDATE' || $cmd == 'DELETE' || $cmd == 'INSERT') { $result = self::$db->affectedRows($result); } // Check if query updated data in the database and run cache handlers if (!empty($result) && preg_match("/^(UPDATE|INSERT INTO|REPLACE INTO|DELETE FROM) " . self::$table_prefix . "(\\w+) /", $query, $m)) { Registry::setChangedTables($m[2]); } } } else { // Lost connection, try to reconnect if (self::tryReconnect()) { return self::query($query); // Assume that the table is broken // Try to repair } elseif (preg_match("/'(\\S+)\\.(MYI|MYD)/", self::$db->error(), $matches)) { self::$db->query("REPAIR TABLE {$matches['1']}"); return self::query($query); } } } self::$raw = false; return $result; }