// Remove \t, \n and \r from queries they may confuse some MySQL version I have heard
$sql_string = str_replace("\t", " ", str_replace("\n", " ", str_replace("\r", " ", $sql_string)));
+ // Starting time
+ $querytimeBefore = array_sum(explode(' ', microtime()));
+
// Run SQL command
$result = @mysql_query($sql_string, $link)
or ADD_FATAL($F." (".$L."):".mysql_error()."<br />
".MYSQL_QUERY_STRING."<br />
".$sql_string);
- // Count this query
- if (!isset($_CONFIG['sql_count'])) $_CONFIG['sql_count'] = 0;
- $_CONFIG['sql_count']++;
+ // Starting time
+ $querytimeAfter = array_sum(explode(' ', microtime()));
+
+ // Calculate query time
+ $queryTime = $querytimeAfter - $querytimeBefore;
- // Debug output
- //* DEBUG: */ print "Query=<pre>".$sql_string."</pre>, affected=<b>".SQL_AFFECTEDROWS()."</b>, numrows=<b>".SQL_NUMROWS($result)."</b><br />\n";
+ // Count this query
+ if (!isset($_CONFIG['sql_count'])) $_CONFIG['sql_count'] = 0;
+ $_CONFIG['sql_count']++;
+
+ // Debug output
+ //* DEBUG: */ print "Query=<pre>".$sql_string."</pre>, affected=<b>".SQL_AFFECTEDROWS()."</b>, numrows=<b>".SQL_NUMROWS($result)."</b><br />\n";
if (($CSS != "1") && ($CSS != "-1") && (isBooleanConstantAndTrue('DEBUG_MODE')) && (DEBUG_SQL)) {
//
// Debugging stuff...
//
- $fp = @fopen(PATH."debug.log", 'a') or mxchange_die("Cannot write debug.log!");
+ $fp = @fopen(PATH."inc/cache/debug.log", 'a') or mxchange_die("Cannot write debug.log!");
if (!isset($OK)) {
// Write first entry
fwrite($fp, "Module=".$GLOBALS['module']."\n");
$OK = true;
}
- fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."): ".str_replace('\r', "", str_replace('\n', " ", $sql_string))."\n");
+ fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."|QUERYTIME:".$queryTime."): ".str_replace('\r', "", str_replace('\n', " ", $sql_string))."\n");
fclose($fp);
}
// SQL num rows
function SQL_NUMROWS($result) {
- if ($result != false) {
+ // Is the result a valid resource?
+ if (is_resource($result)) {
+ // Get the count of rows from database
$lines = @mysql_num_rows($result);
- if (empty($lines)) $lines = "0";
+ // Is the result empty? Then we have an error!
+ if (empty($lines)) $lines = "0";
} else {
// No resource given, no lines found!
$lines = "0";
return $connect;
}
// SQL select database
-function SQL_SELECT_DB($DB, $link, $F, $L) {
+function SQL_SELECT_DB($dbName, $link, $F, $L) {
$select = false;
if (is_resource($link)) {
- $select = @mysql_select_db($DB, $link) or ADD_FATAL($F." (".$L."):".mysql_error());
+ $select = @mysql_select_db($dbName, $link) or ADD_FATAL($F." (".$L."):".mysql_error());
}
return $select;
}
// Escape a string for the database
function SQL_ESCAPE($str) {
global $link;
+
if (!is_resource($link)) {
// Fall-back to addslashes() when there is no link
return addslashes($str);
- }
+ } // END - if
if (function_exists('mysql_real_escape_string')) {
// The new and improved version
// Return the result
return SQL_QUERY_ESC($SQL, array(bigintval($id)), __FILE__, __LINE__);
}
+// ALTER TABLE wrapper function
+function SQL_ALTER_TABLE($sql, $F, $L) {
+ // Shall we add?
+ if (eregi("ADD", $sql) > 0) {
+ // Extract table name
+ $tableArray = explode(" ", $sql);
+ $tableName = str_replace("`", "", $tableArray[2]);
+
+ // And column name as well
+ $columnName = str_replace("`", "", $tableArray[4]);
+
+ // Get column information
+ $result = SQL_QUERY_ESC("SHOW COLUMNS FROM %s LIKE '%s'",
+ array($tableName, $columnName), __FILE__, __LINE__);
+
+ // Do we have no entry?
+ if (SQL_NUMROWS($result) == 0) {
+ // Do the query
+ return SQL_QUERY($sql, $F, $L, false);
+ } // END - if
+ } else {
+ // Send it to the SQL_QUERY() function
+ return SQL_QUERY($sql, $F, $L, false);
+ }
+}
//
?>