Location: PHPKode > projects > HandicappingOS > phpBRIS/includes/misc.inc
<?

/*
  HandicappingOS, Open Source Handicapping Solutions
  http://www.handicappingOS

  Copyright (c) 2005 Richard Rosa

  Released under the GNU General Public License
*/

function get_var_from_getpost ($varname) {

    // gets our needed variables from the previous POST or GET
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       return $_POST[$varname];
       }
    elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
       return $_GET[$varname];
       }
    else {
        return "";
        }
}

function execute_sql($sql) { 
		
		global $conn, $debug;
		
		$recordSet = &$conn->Execute($sql);

		if ($debug) { 
	        echo "<br>sql = " . $sql;
        	}	
	
		if ($recordSet == false)
		{
			if (_DEBUG == 1)
			{
				show_sql_error($conn->ErrorMsg(), $sql);
				die();
			}	
			else
			{
				errorLogging($conn->ErrorMsg(), $sql);
				die("failed");
			}
		}	
		
		return $recordSet;
}

function execute_chart_sql($sql) { 
		
		global $chartconn, $debug;
		
		$recordSet = &$chartconn->Execute($sql);

		if ($debug) { 
	        echo "<br>sql = " . $sql;
        	}	
	
		if ($recordSet == false)
		{
			if (_DEBUG == 1)
			{
				show_sql_error($conn->ErrorMsg(), $sql);
				die();
			}	
			else
			{
				errorLogging($conn->ErrorMsg(), $sql);
				die("failed");
			}
		}	
		
		return $recordSet;
}


function execute_sql_into_array($sql) { 
		
		global $conn, $debug;
		
		$recordSet = &$conn->Execute($sql);

		if ($debug) { 
	        echo "<br>sql = " . $sql;
        	}	
	
		if ($recordSet == false)
		{
			if (_DEBUG == 1)
			{
				show_sql_error($conn->ErrorMsg(), $sql);
				die();
			}	
			else
			{
				errorLogging($conn->ErrorMsg(), $sql);
				die("failed");
			}
		}	
		//while (!$recordSet->EOF) {
			
		return $recordSet;
}


function errorLogging($error, $sql) {
  	error_log($error . "\nSQL - " . $sql . "\n");
    echo "<br>" . $error . "<br>SQL - " . $sql . "\n";
  	}
 

function show_array ($printableArray, $title="") {
   // for debugging
   if ($title) { 
	   print "<br><br><b>$title</b>"; 
   	   }
   print "<pre>";
   print_r($printableArray);
   print "</pre>";
}

function debug_array ($printableArray, $title="") {
   // for debugging
   if ($title) { 
	   $payload .= "<br><br><b>$title</b>"; 
   	   }
   $payload .= "<pre>";
   $payload .= print_r($printableArray, TRUE);
   $payload .= print "</pre>";
   
   return $payload; 

}


function show_class($obj) { 
    $class_vars = get_class_vars(get_class($obj));
    foreach ($class_vars as $name => $value) {
        echo "<br>$name : ";
        if (is_Array($obj->$name)) {
            show_array($obj->$name);
            }
        else {
            echo htmlSpecialChars($obj->$name);
            }
        }
	} 
	
function logger($flag = "none", $entry_line) { 
	global $fp; 
	fputs($fp, date("m/d/y G:i:s") . " -- " . $flag . " -- " . $entry_line . "\n");
	}	
	
function debugprint ($payload, $level) {
	
	global $debug; 
	
	if ($debug >= $level) { 
		echo "$payload"; 
		}
}	    

//String functions
function left($str, $cnt)
{
	return substr($str, 0, $cnt);
}

function right($str, $cnt)
{
	return substr($str, strlen($str)-$cnt);
}

function startsWith($haystack, $needle)
{
    return substr($haystack, 0, strlen($needle)) == $needle;
}

function endsWith($haystack, $needle)
{
	return right($haystack, strlen($needle)) == $needle;
}

function chopFirst($str, $n=1)
{
	return substr($str, $n);
}

function chopLast($str, $n=1)
{
	return substr($str, 0, strlen($str)-$n);
}

//DB Layer
function dbOutput($string)
{
	return stripslashes($string);
}

function dbInput($string)
{
	return addslashes($string);
}

function dbInsertId()
{
	return mysql_insert_id();
}

function dbConnect($server=DB_SERVER,
                   $userName=DB_SERVER_USERNAME,
                   $password=DB_SERVER_PASSWORD,
                   $database=DB_DATABASE)
{
	global $dbLink;

	if (USE_PCONNECT == true)
	{
		$dbLink = mysql_pconnect($server, $userName, $password);
	}
	else
	{
		$dbLink = mysql_connect($server, $userName, $password);
	}

	if ($dbLink) mysql_select_db($database);
	return $dbLink;

}

function dbClose()
{
	global $dbLink;
	return mysql_close($dbLink);
}

function sendDbError($e)
{
	$message = "Une erreur mySql s'est produite à ".date("H:i:s")."!<br><br><br>\nErreur : ";
	$message .= $e;
	$message .= "<br><br>Page : " . $_SERVER['PHP_SELF'];
	echo $message;
}

function dbQuery($dbQuery)
{
	global $dbLink;

	$time_start = microtime();
	$result = mysql_query($dbQuery, $dbLink) or sendDbError($dbQuery . mysql_errno() . mysql_error());

	if($_GET['debug_mode']==1)
	{
		$query_time = microtime_diff(microtime(), $time_start) * 1000;
		echo "<br>Requete : \n$dbQuery (".dbNumRows($result)." resultats, $query_time ms)";
	}

	return $result;
}

function dbFetchArray($dbQuery)
{
	return mysql_fetch_array($dbQuery);
}

function dbValue($dbQuery)
{
	$t=dbFetchArray(dbQuery($dbQuery));
	return $t[0];
}

function dbNumRows($dbQuery)
{
	return mysql_num_rows($dbQuery);
}

function dbQueryArray($dbQuery)
{
	$q=dbQuery($dbQuery);
	$i=0;
	while($d=dbFetchArray($q))
	{
		while(list($k, $v) = each($d))
		{
			if(!is_numeric($k))
				$results[$i][$k]=$v;
		}
		$i++;
	}

	return $results;
}

function dbQueryArrayNum($dbQuery)
{
	$q=dbQuery($dbQuery);
	$i=0;
	while($d=dbFetchArray($q))
	{
		while(list($k, $v) = each($d))
		{
			if(is_numeric($k))
				$results[$i][$k]=$v;
		}
		$i++;
	}

	return $results;
}

function dbQueryLine($dbQuery)
{
	$d=dbQueryArray($dbQuery);
	return $d[0];
}

function dbQueryLineNum($dbQuery)
{
	$d=dbQueryArrayNum($dbQuery);
	return $d[0];
}

function dbValueArray($dbQuery)
{
	$q=dbQuery($dbQuery);
	while($d=dbFetchArray($q))
	{
		$r[] = $d[0];
	}
	return $r;
}
?>
Return current item: HandicappingOS