function driver_db_query($query) { global $db_conn; static $reconnect_attempts = 0; $result = mysql_query($query, $db_conn); if (empty($result)) { // Lost connection, try to reconnect (max - 3 times) if ((mysql_errno($db_conn) == 2013 || mysql_errno($db_conn) == 2006) && $reconnect_attempts < 3) { $db_conn = db_initiate(Registry::get('config.db_host'), Registry::get('config.db_user'), Registry::get('config.db_password'), Registry::get('config.db_name')); $reconnect_attempts++; $result = driver_db_query($query); // Assume that the table is broken // Try to repair } elseif (preg_match("/'(\\S+)\\.(MYI|MYD)/", mysql_error($db_conn), $matches)) { $result = mysql_query("REPAIR TABLE {$matches['1']}", $db_conn); } } return $result; }
/** * Execute query * * @param string $query unparsed query * @param mixed ... unlimited number of variables for placeholders * @return boolean always true, dies if problem occured */ function db_query($query) { Registry::set('runtime.database.long_query', false); $args = func_get_args(); $query = db_process($query, array_slice($args, 1)); if (empty($query)) { return false; } if (defined('DEBUG_QUERIES')) { fn_print_r($query); } $time_start = microtime(true); file_put_contents("D:/query.txt", $query . "\r\n", FILE_APPEND); $result = driver_db_query($query); $time_exec = microtime(true) - $time_start; if (defined('PROFILER')) { Profiler::set_query($query, $time_exec); } // Check if query updates data in the database if ($time_exec > 30.003) { Registry::set('runtime.database.long_query', true); Registry::set('runtime.database.last_query', $query); } if ($result === true) { // true returns for success insert/update/delete query // Check if it was insert statement with auto_increment value if (Registry::is_exist('revisions') && ($i_id = Registry::get('revisions.db_insert_id')) && !Registry::get('revisions.working')) { Registry::set('revisions.db_insert_id', null); return $i_id; } elseif ($i_id = driver_db_insert_id()) { return $i_id; } } db_error($result, $query); return $result; }