static function query($query, $call_pos = '') { self::$db_result = false; if (!empty($query)) { //if (isset($_REQUEST["ebug"]) && intval($_REQUEST["ebug"]) > 0) { if (DEBUG) { $rtime = microtime(); $rtime = explode(" ", $rtime); $rtime = $rtime[1] + $rtime[0]; $start_time = $rtime; } if (_DB_REPLICATE and preg_match("/^(?:\\()?select/i", $query) and self::$replicate_query) { if (!self::$slave_connect) { self::$slave_connect = self::db_connect(DB_SLAVE_SERVER, DB_SLAVE_USER, DB_SLAVE_PASSWORD, DB_SLAVE_NAME); } $connection_switch = self::$slave_connect; } else { if (!self::$master_connect) { self::$master_connect = self::db_connect(DB_MASTER_SERVER, DB_MASTER_USER, DB_MASTER_PASSWORD, DB_MASTER_NAME); } $connection_switch = self::$master_connect; } self::$db_connect_id = $connection_switch; if (!(self::$db_result = @mysql_query($query, self::$db_connect_id))) { self::$errorCode = mysql_errno(self::$db_connect_id); //if(isset($_REQUEST["ebug"]) && intval($_REQUEST["ebug"]) > 0){ if (DEBUG) { if (self::$showErrorMsg) { echo '<p><font face="Courier New,Courier" size=3><b>' . mysql_error(self::$db_connect_id) . ' in ' . $query . '</b></font><br><pre style="text-align: left">' . var_export(debug_backtrace(), true) . '</pre>'; exit; } } else { if (self::$showErrorMsg) { echo '<p><font face="Courier New,Courier" size=3><b>' . mysql_error(self::$db_connect_id) . ' in ' . $query . '</b></font><br><pre style="text-align: left">' . var_export(debug_backtrace(), true) . '</pre>'; //echo '<p><font face="Courier New,Courier" size=3><b>Có lỗi khi truy vấn CSDL!</b></font><br>'; exit; } } } self::$db_num_queries++; //if (isset($_REQUEST["ebug"]) && intval($_REQUEST["ebug"]) > 0) { if (DEBUG) { if (class_exists('Module') && Module::$name != '') { $module_name = Module::$name; } else { $module_name = "-- Enbac system"; } $effect_rows = mysql_affected_rows(self::$db_connect_id); $rtime = microtime(); $rtime = explode(" ", $rtime); $rtime = $rtime[1] + $rtime[0]; $end_time = $rtime; $doing_time = round($end_time - $start_time, 5) . "s"; if (preg_match("/^select/i", $query)) { $eid = mysql_query("EXPLAIN {$query}", self::$db_connect_id); CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FFE8F3' align='center'>\n\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t <td colspan='8' style='font-size:14px' bgcolor='#FFC5Cb'><b>Select Query</b> -- Module : <span style='color:#FF8B00;font-weight:bold'>{$module_name}</span>" . ($call_pos ? "<br /><b>Run at:</b> {$call_pos}<br />" : "") . "</td>\n\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t <td colspan='8' style='font-family:courier, monaco, arial;font-size:14px;color:black'>{$query}</td>\n\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t<tr bgcolor='#FFC5Cb'>\n\t\t\t\t\t\t\t\t\t\t <td><b>table</b></td><td><b>type</b></td><td><b>possible_keys</b></td>\n\t\t\t\t\t\t\t\t\t\t <td><b>key</b></td><td><b>key_len</b></td><td><b>ref</b></td>\n\t\t\t\t\t\t\t\t\t\t <td><b>rows</b></td><td><b>Extra</b></td>\n\t\t\t\t\t\t\t\t\t\t</tr>\n"; while ($array = mysql_fetch_array($eid)) { $type_col = '#FFFFFF'; if ($array['type'] == 'ref' or $array['type'] == 'eq_ref' or $array['type'] == 'const') { $type_col = '#D8FFD4'; } else { if ($array['type'] == 'ALL') { $type_col = '#FFEEBA'; } } CGlobal::$query_debug .= "<tr bgcolor='#FFFFFF'>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['table']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td bgcolor='{$type_col}'>{$array['type']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['possible_keys']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key_len']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['ref']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['rows']} </td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['Extra']} </td>\n\t\t\t\t\t\t\t\t\t\t\t</tr>\n"; } CGlobal::$query_time += $doing_time; if ($doing_time > 0.1) { $doing_time = "<span style='color:red'><b>{$doing_time}</b></span>"; } CGlobal::$query_debug .= "<tr>\n\t\t\t\t\t\t\t\t\t\t\t <td colspan='8' bgcolor='#FFD6DC' style='font-size:14px'><b>MySQL time</b>: {$doing_time}</b></td>\n\t\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t\t </table>\n<br />\n"; } else { CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE' align='center'>\n\t\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><b>Non Select Query</b> -- Module : <span style='color:#FF8B00;font-weight:bold'>{$module_name}</span>" . ($call_pos ? "<br /><b>Run at:</b> {$call_pos}" : "") . "</td>\n\t\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-family:courier, monaco, arial;font-size:14px'>{$query}</td>\n\t\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><b>MySQL time</b>: {$doing_time}</span></td>\n\t\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t\t</table><br />\n\n"; } } } return self::$db_result; }
static function query($query, $call_pos = '') { self::$db_result = false; if (!empty($query)) { if (DEBUG) { $rtime = microtime(); $rtime = explode(" ", $rtime); $rtime = $rtime[1] + $rtime[0]; $start_time = $rtime; } if (_DB_REPLICATE and preg_match("/^(?:\\()?select/i", $query) and self::$replicate_query) { if (!self::$slave_connect) { self::$slave_connect = self::db_connect(DB_SLAVE_SERVER, DB_SLAVE_USER, DB_SLAVE_PASSWORD, DB_SLAVE_NAME); } $connection_switch = self::$slave_connect; } else { if (!self::$master_connect) { self::$master_connect = self::db_connect(DB_MASTER_SERVER, DB_MASTER_USER, DB_MASTER_PASSWORD, DB_MASTER_NAME); } $connection_switch = self::$master_connect; } self::$db_connect_id = $connection_switch; if (!(self::$db_result = @mysql_query($query, self::$db_connect_id))) { $note = $call_pos . "\n[Error : " . mysql_error(self::$db_connect_id) . "]\n[Error Code : ]" . mysql_errno(self::$db_connect_id) . ""; self::writeLogFile($query, null, $note, 'error_sql_'); if (DEBUG) { //echo '<p><font face="Courier New,Courier" size=3><b>'.mysql_error(self::$db_connect_id).' in '.$query .'</b></font><br>'.($call_pos?"<b>Run at:</b> $call_pos":""); $back_strace_arr = debug_backtrace(); $back_strace = array(); foreach ($back_strace_arr as $b_trace) { if (isset($b_trace['class']) && $b_trace['class'] == 'DB') { $back_strace[] = array('file' => $b_trace['file'], 'line' => $b_trace['line'], 'function' => $b_trace['class'] . "::" . $b_trace['function'], 'args' => $b_trace['args']); } } krsort($back_strace); echo '<p><font face="Courier New,Courier" size=3><b>' . mysql_error(self::$db_connect_id) . ' in ' . $query . '</b></font><br>' . "<pre>" . print_r($back_strace, 1) . "</pre>"; exit; } else { echo '<p><font face="Courier New,Courier" size=3><b>Có lỗi khi truy vấn csdl</b></font><br>'; exit; } } self::$db_num_queries++; if (DEBUG) { if (class_exists('Module') && Module::$name != '') { $module_name = Module::$name; } else { $module_name = "-- Enbac system"; } $effect_rows = mysql_affected_rows(self::$db_connect_id); $rtime = microtime(); $rtime = explode(" ", $rtime); $rtime = $rtime[1] + $rtime[0]; $end_time = $rtime; $doing_time = round($end_time - $start_time, 5) . "s"; if (preg_match("/^select/i", $query)) { $eid = mysql_query("EXPLAIN {$query}", self::$db_connect_id); CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FFE8F3' align='center'>\r\n\t\t\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t\t\t <td colspan='8' style='font-size:14px' bgcolor='#FFC5Cb'><b>Select Query</b> -- Module : <span style='color:#FF8B00;font-weight:bold'>{$module_name}</span>" . ($call_pos ? "<br /><b>Run at:</b> {$call_pos}<br />" : "") . "</td>\r\n\t\t\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t\t\t <td colspan='8' style='font-family:courier, monaco, arial;font-size:14px;color:black'>{$query}</td>\r\n\t\t\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t\t\t<tr bgcolor='#FFC5Cb'>\r\n\t\t\t\t\t\t\t\t\t\t <td><b>table</b></td><td><b>type</b></td><td><b>possible_keys</b></td>\r\n\t\t\t\t\t\t\t\t\t\t <td><b>key</b></td><td><b>key_len</b></td><td><b>ref</b></td>\r\n\t\t\t\t\t\t\t\t\t\t <td><b>rows</b></td><td><b>Extra</b></td>\r\n\t\t\t\t\t\t\t\t\t\t</tr>\n"; while ($array = mysql_fetch_array($eid)) { $type_col = '#FFFFFF'; if ($array['type'] == 'ref' or $array['type'] == 'eq_ref' or $array['type'] == 'const') { $type_col = '#D8FFD4'; } else { if ($array['type'] == 'ALL') { $type_col = '#FFEEBA'; } } CGlobal::$query_debug .= "<tr bgcolor='#FFFFFF'>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['table']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td bgcolor='{$type_col}'>{$array['type']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['possible_keys']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key_len']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['ref']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['rows']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['Extra']} </td>\r\n\t\t\t\t\t\t\t\t\t\t\t</tr>\n"; } CGlobal::$query_time += $doing_time; if ($doing_time > 0.1) { $doing_time = "<span style='color:red'><b>{$doing_time}</b></span>"; } CGlobal::$query_debug .= "<tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <td colspan='8' bgcolor='#FFD6DC' style='font-size:14px'><b>MySQL time</b>: {$doing_time}</b></td>\r\n\t\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t\t </table>\n<br />\n"; } else { CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE' align='center'>\r\n\t\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><b>Non Select Query</b> -- Module : <span style='color:#FF8B00;font-weight:bold'>{$module_name}</span>" . ($call_pos ? "<br /><b>Run at:</b> {$call_pos}" : "") . "</td>\r\n\t\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-family:courier, monaco, arial;font-size:14px'>{$query}</td>\r\n\t\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><b>MySQL time</b>: {$doing_time}</span></td>\r\n\t\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t\t</table><br />\n\n"; } } } return self::$db_result; }