// SQL queries
function SQL_QUERY ($sql_string, $F, $L) {
- global $link, $OK;
+ global $OK;
// Link is up?
- if (!is_resource($link)) return false;
+ if (!SQL_IS_LINK_UP()) return false;
// 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)));
// Run SQL command
//* DEBUG: */ echo $sql_string."<br />\n";
- $result = mysql_query($sql_string, $link)
+ $result = mysql_query($sql_string, SQL_GET_LINK())
or addFatalMessage($F." (".$L."):".mysql_error()."<br />
Query string:<br />
".$sql_string);
// SQL affected rows
function SQL_AFFECTEDROWS() {
- global $link;
-
// Valid link resource?
- if (!is_resource($link)) return false;
+ if (!SQL_IS_LINK_UP()) return false;
// Get affected rows
- $lines = mysql_affected_rows($link);
+ $lines = mysql_affected_rows(SQL_GET_LINK());
// Return it
return $lines;
// SQL connect
function SQL_CONNECT ($host, $login, $password, $F, $L) {
+ // Try to connect
$connect = mysql_connect($host, $login, $password) or addFatalMessage($F." (".$L."):".mysql_error());
- return $connect;
+
+ // Set the link resource
+ SQL_SET_LINK($connect);
}
// SQL select database
-function SQL_SELECT_DB ($dbName, $link, $F, $L) {
+function SQL_SELECT_DB ($dbName, $F, $L) {
// Is there still a valid link? If not, skip it.
- if (!is_resource($link)) return false;
+ if (!SQL_IS_LINK_UP()) return false;
- return mysql_select_db($dbName, $link) or addFatalMessage($F." (".$L."):".mysql_error());
+ // Return the result
+ return mysql_select_db($dbName, SQL_GET_LINK()) or addFatalMessage($F." (".$L."):".mysql_error());
}
// SQL close link
-function SQL_CLOSE (&$link, $F, $L) {
- if (!is_resource($link)) {
+function SQL_CLOSE ($F, $L) {
+ if (!SQL_IS_LINK_UP()) {
// Skip double close
return false;
} // END - if
} // END - if
// Close database link and forget the link
- $close = mysql_close($link) or addFatalMessage($F." (".$L."):".mysql_error());
- $link = null;
+ $close = mysql_close(SQL_GET_LINK()) or addFatalMessage($F." (".$L."):".mysql_error());
+
+ // Close link
+ SQL_SET_LINK(null);
+
+ // Return the result
return $close;
}
// SQL string escaping
function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true) {
- global $link;
-
// Link is there?
- if (!is_resource($link)) return false;
+ if (!SQL_IS_LINK_UP()) return false;
// Init variable
$query = "failed";
// Get ID from last INSERT command
function SQL_INSERTID () {
- global $link;
- if (!is_resource($link)) return false;
+ if (!SQL_IS_LINK_UP()) return false;
return mysql_insert_id();
}
// Escape a string for the database
function SQL_ESCAPE ($str, $secureString=true,$strip=true) {
- global $link;
-
// Secure string first? (which is the default behaviour!)
if ($secureString) {
// Then do it here
$str = secureString($str, $strip);
} // END - if
- if (!is_resource($link)) {
+ if (!SQL_IS_LINK_UP()) {
// Fall-back to smartAddSlashes() when there is no link
return smartAddSlashes($str);
} elseif (function_exists('mysql_real_escape_string')) {
// The new and improved version
//* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):str={$str}<br />\n";
- return mysql_real_escape_string($str, $link);
+ return mysql_real_escape_string($str, SQL_GET_LINK());
} elseif (function_exists('mysql_escape_string')) {
// The obsolete function
- return mysql_escape_string($str, $link);
+ return mysql_escape_string($str, SQL_GET_LINK());
} else {
// If nothing else works, fall back to smartAddSlashes()
return smartAddSlashes($str);
return $result;
}
+// Getter for SQL link
+function SQL_GET_LINK () {
+ // Init link
+ $link = null;
+
+ // Is it in the globals?
+ if (isset($GLOBALS['sql_link'])) {
+ // Then take it
+ $link = $GLOBALS['sql_link'];
+ } // END - if
+
+ // Return it
+ return $link;
+}
+
+// Setter for link
+function SQL_SET_LINK ($link) {
+ // Set it
+ $GLOABLS['sql_link'] = $link;
+}
+
+// Checks if the link is up
+function SQL_IS_LINK_UP () {
+ // Get the link
+ $link = SQL_GET_LINK();
+
+ // Return the result
+ return (is_resource($link));
+}
+
//
?>
// Filter for flushing all new filters to the database
function FILTER_FLUSH_FILTERS () {
- global $filters, $counter, $link, $loadedFilters, $SQLs;
+ global $filters, $counter, $loadedFilters, $SQLs;
// Clear all previous SQL queries
$SQLs = array();
// Is a database link here and not in installation mode?
- if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
+ if ((!SQL_IS_LINK_UP()) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
// Abort here
addFatalMessage(getMessage('FILTER_FLUSH_FAILED_NO_DATABASE'), array($filterFunction, $filterName));
return false;
************************************************************************/
// Global variable stuff
-global $link, $frame;
+global $frame;
// Some security stuff...
if (!defined('__SECURITY')) {
// Add title decorations? (left)
if (!defined('__PAGE_TITLE')) {
// Config and database connection valid?
- if ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 1) && (is_resource($link)) && (isset($db))) {
+ if ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 1) && (SQL_IS_LINK_UP())) {
// Title decoration enabled?
if ((getConfig('enable_title_deco') == "Y") && (getConfig('title_left') != "")) $TITLE .= trim(getConfig('title_left'))." ";
LOAD_TEMPLATE("metadata");
// Add meta description to header
- if ((isBooleanConstantAndTrue('mxchange_installed')) && (isBooleanConstantAndTrue('admin_registered')) && (isset($db)) && (is_resource($link))) {
+ if ((isBooleanConstantAndTrue('mxchange_installed')) && (isBooleanConstantAndTrue('admin_registered')) && (SQL_IS_LINK_UP())) {
// Add meta description not in admin and login module and when the script is installed
META_DESCRIPTION($GLOBALS['module'], $GLOBALS['what']);
} // END - if
$SQLs = array();
// Connect to MySQL server
- $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
- if ($link) {
+ SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
+ if (SQL_IS_LINK_UP()) {
// Seems to work, also right database?
- $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__);
- if ($db) {
+ if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) {
// Automatically run install.sql
if ((FILE_READABLE($_POST['spath']."install/tables.sql")) && (FILE_READABLE($_POST['spath']."install/menu-".GET_LANGUAGE().".sql"))) {
// Both exists so import them
// Call-back function for running shutdown functions and close database connection
function __SHUTDOWN_HOOK () {
- global $link;
-
// Call the filter chain 'shutdown'
RUN_FILTER('shutdown', null, false);
- if (is_resource($link)) {
+ if (SQL_IS_LINK_UP()) {
// Close link
- SQL_CLOSE($link, __FILE__, __LINE__);
+ SQL_CLOSE(__FILE__, __LINE__);
} else {
// No database link
addFatalMessage(getMessage('NO_DB_LINK'));
if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) {
// Connect to DB
- global $link;
- $link = SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
+ SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__);
// Is the link valid?
- if (is_resource($link)) {
- // Choose the database
- global $db;
- $db = SQL_SELECT_DB($MySQL['dbase'], $link, __FILE__, __LINE__);
-
+ if (SQL_IS_LINK_UP()) {
// Is it a valid resource?
- if ($db === true) {
+ if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) {
// This is required for extension 'optimize' to work
define('__DB_NAME', $MySQL['dbase']);
// No link to database!
addFatalMessage(getMessage('NO_DB_LINK'));
- $db = false;
}
} else {
// Add language system
// Set other missing variables
if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0";
- $link = false; // No database link by default
// Include required files
LOAD_INC_ONCE("inc/databases.php");
if ((getConfig('maintenance') == "Y") && (!IS_ADMIN()) && ($GLOBALS['module'] != "admin")) {
// Maintain mode is active and you are no admin
addFatalMessage(getMessage('LANG_DOWN_MAINTAINCE'));
-} elseif (($link) && ($db) && (getTotalFatalErrors() == 0)) {
+} elseif ((SQL_IS_LINK_UP()) && (getTotalFatalErrors() == 0)) {
// Construct module name
define('__MODULE', sprintf("inc/modules/%s.php", SQL_ESCAPE($GLOBALS['module'])));