<?
if (!$SITE['database']) {

	$SITE['database'] 			 = 1;

	function stripslashes2($string) {
	
		$string 			 = str_replace("\\", "", $string);
	
		return $string;
	}
	
	function connecte_db($forum = 0) {
	global $SITE, $INFO;
	
			$host			 = 'mysql5-5.perso';
			$base			 = 'qualitesfsite';
			$login			 = 'qualitesfsite';
			$password		 = 'nuza1837';
		
		$db 				 = mysql_connect($host, $login, $password) 
	
		or die('Problème de connexion au serveur MYSQL...');

		if (!mysql_select_db($base)) {
	
			mysql_close($db);

			die('Problème de connexion à la base de données MYSQL...');	
		}
	
		return $db;
	}
	
	function connexion_sql($forum = 0) {
	global $webmasters_db, $SITE;
	
		if (($SITE['connexion_sql'])) 	 { return 0; }
	
		else {
			$SITE['connexion_sql'] = 1;
		
			$webmasters_db = connecte_db($forum);
	
			return 1;
		}
	}
	
	function deconnexion_sql() {
	global $SITE, $webmasters_db;
	
		if (!($SITE['connexion_sql'])) { return 0; }
	
		else {
			$SITE['connexion_sql'] = 0;
	
			mysql_close($webmasters_db);
	
			return 1;
		}
	}

	function requete_sql($query, $fline = 0, $fname = "") {
	global $SITE, $webmasters_db; 

		connexion_sql();
     	
		if (!($SITE['connexion_sql'])) { die ("<u>Problème d'exécution de la requête</u> : <b>$query</b><br><br>Vous avez oublié de vous connecter via la fonction <b>connexion_sql()</b>..."); } 
      
         	$i = 1; 

	        $tquery = substr(strtoupper($query),0,6); 
      
	        if (($SITE['ip_admin'] == $_SERVER['REMOTE_ADDR']) and ($SITE['debug_sql']==1)) { 

			$status = explode('  ',mysql_stat($webmasters_db));
			echo "<hr><b><u><font color=red>ETAT DU SERVEUR SQL</font></u> : <font color=green>";
			print_r($status);
			echo "</font></b> : <br><br>";
			echo "<b><u><font color=red>REQUETE SQL</font></u> (".basename($fname)." - ligne n°$fline) : <font color=blue>".$query."</font></b><hr><br>\r\n";
	        }      
	
		$result = mysql_query($query,$webmasters_db);

		if ($SITE['pause_sql']) {

			//-- Pause de 1/10 de seconde pour REPOSER LE SERVEUR quand il y a TROP DE REQUETES A LA SUITE -->
			
			usleep($SITE['pause_sql']);
		}

		if (!$result) {
	
			deconnexion_sql();
			die('Problème dans la requête MYSQL...'); 
	
		} else {
	
			if ($tquery == 'SELECT') {

				if (!$SITE['no_sql_assoc']) {
	
					while ($fetch_x = mysql_fetch_array($result, MYSQL_ASSOC)) {
	
						// $num[$i] = stripslashes($fetch_x);
	
						$num[$i] = $fetch_x;
	
						$i++;
					}

				} else {

					while ($fetch_x = mysql_fetch_array($result)) {
	
						// $num[$i] = stripslashes($fetch_x);
	
						$num[$i] = $fetch_x;
	
						$i++;
					}
				}
	
				mysql_free_result($result);
	
			} else $i = 2;
		}
	
		$num['count'] = $i-1;
	
		if (!$num['count']) { $num = 0; }

		deconnexion_sql();
	
		return $num;
	}
	
	function nb_results($table, $where, $etoile = '*') {
	global $webmasters_db, $SITE;

		connexion_sql();
	
		if ($where != '') { $query = "SELECT count($etoile) AS number FROM {$table} WHERE {$where}"; }
		else 		  { $query = "SELECT count($etoile) AS number FROM {$table}"; }
		
		if ($SITE['debug_sql']) { echo $query; }
		
		$num 	= requete_sql($query);
		$total  = $num[1]['number'];

		deconnexion_sql();
	
		return $total;
	}
	
	function inserer_variables_to_table_sql ($chaine_variables_globales, $table) {
	global $webmasters_db;

		connexion_sql();
	
		$requete = "INSERT INTO $table ($chaine_variables_globales) VALUES (";
	
		$tableau = explode(", ", $chaine_variables_globales);
		$n 	 = count ($tableau);
	
		for ($i = 0; $i < $n-1; $i++) {
			$requete .= "'".addslashes(stripslashes2($GLOBALS[$tableau[$i]]))."', ";
			
		}
	
		$requete .= "'".addslashes(stripslashes2($GLOBALS[$tableau[$n-1]]))."')";
		
		if (mysql_query($requete,$webmasters_db))	{ $id = mysql_insert_id(); }
		else						{ $id = 0; }

		deconnexion_sql();

		return $id;
	}
	
	function modifier_variables_in_table_sql ($id, $chaine_variables_globales, $table) {

		connexion_sql();
	
		$requete = "UPDATE $table SET ";
	
		$tableau = explode(", ", $chaine_variables_globales);
		$n 	 = count ($tableau);
	
		for ($i = 0; $i < $n-1; $i++) {
			$requete .= $tableau[$i]."='".addslashes(stripslashes2($GLOBALS[$tableau[$i]]))."', ";
			
		}
	
		$requete .= $tableau[$n-1]."='".addslashes(stripslashes2($GLOBALS[$tableau[$n-1]]))."' WHERE id=$id";
	
		deconnexion_sql();		

		return requete_sql($requete);
	}
}	
?>