Petite class mysql avec fonction de recherche, ect

Contenu du snippet

Voila, c'est une petite class mysql assez complete, si vous l'ameliorer ou avez des idées dites le moi.

Source / Exemple :


<?
class mysql {
	var $Host="";
	var $User="";
	var $Pass="";
	var $Database="";
	var $refConnect=0;
	var $connected=0;
	var $precQueryId;
	var $enreg=array();
	var $errno=0;
	var $error="";
	var $queryExec=0;
	var $tbsInfo=array();

	function mysql($arrayConfig) {
		$this->Host=$arrayConfig["HOST"];
		$this->User=$arrayConfig["USER"];
		$this->Pass=$arrayConfig["PASSWORD"];
		$this->Database=(isset($arrayConfig["DATABASE"]))?$arrayConfig["DATABASE"]:"";
		if($this->Database) $this->select_db($this->Database);
	}
	function connect() {
		if(!$this->connected) {
			$this->refConnect=@mysql_connect($this->Host,$this->User,$this->Pass);
			$this->errno=mysql_errno();
			$this->error=mysql_error();
			if(!$this->refConnect) $this->erreur("la connection.");
			else $this->connected=1;
		}
	}
	function select_db($nom) {
		$this->Database=$nom;
		if(!$this->connected) $this->connect();
		$selected=@mysql_select_db($nom,$this->refConnect);
		if(!$selected) $this->erreur("la sélection de la base `$nom`");
		else $this->buildTbsInfo();
	}
	function buildTbsInfo() {
		unset($this->tbsInfo);
		$this->query("SHOW TABLE STATUS FROM `$this->Database`");
		$nbrTbs=0;
		$this->tbsInfo["Number_Of_Tables"]=0;
		while($tmp_var=$this->nextRecord()) {
			$this->tbsInfo["Tables"][]=$tmp_var["Name"];
			$this->tbsInfo[$tmp_var["Name"]]["Avg_Row_Length"]=$tmp_var["Avg_row_length"];
			$this->tbsInfo[$tmp_var["Name"]]["Auto_Increment"]=isset($tmp_var["Auto_increment"])?$tmp_var["Auto_increment"]:"";
			$this->tbsInfo[$tmp_var["Name"]]["Check_Time"]=isset($tmp_var["Check_time"])?$tmp_var["Check_time"]:"";
			$this->tbsInfo[$tmp_var["Name"]]["Comment"]=$tmp_var["Comment"];
			$this->tbsInfo[$tmp_var["Name"]]["Create_Time"]=$tmp_var["Create_time"];
			$this->tbsInfo[$tmp_var["Name"]]["Data_Free"]=$tmp_var["Data_free"];
			$this->tbsInfo[$tmp_var["Name"]]["Data_Length"]=$tmp_var["Data_length"];
			$this->tbsInfo[$tmp_var["Name"]]["Index_Length"]=$tmp_var["Index_length"];
			$this->tbsInfo[$tmp_var["Name"]]["Max_Data_Length"]=$tmp_var["Max_data_length"];
			$this->tbsInfo[$tmp_var["Name"]]["Row_Format"]=$tmp_var["Row_format"];
			$this->tbsInfo[$tmp_var["Name"]]["Rows"]=$tmp_var["Rows"];
			$this->tbsInfo[$tmp_var["Name"]]["Type"]=$tmp_var["Type"];
			$this->tbsInfo[$tmp_var["Name"]]["Update_Time"]=isset($tmp_var["Update_time"])?$tmp_var["Update_time"]:"";
			$nbrTbs++;
		}
		$this->tbsInfo["Number_Of_Tables"]=$nbrTbs;
	}
	function getTbInfo($nom) {
		$this->query("DESCRIBE `$nom`");
		$nbrChamps=0;
		$tbInfo["Number_Of_Fields"]=0;
		while($tmp_var=$this->nextRecord()) {
			$tbInfo["Champs"][]=$tmp_var["Field"];
			$tbInfo[$tmp_var["Field"]]["Type"]=$tmp_var["Type"];
			$tbInfo[$tmp_var["Field"]]["Null"]=isset($tmp_var["Null"])?$tmp_var["Null"]:"";
			$tbInfo[$tmp_var["Field"]]["Key"]=isset($tmp_var["Key"])?$tmp_var["Key"]:"";
			$tbInfo[$tmp_var["Field"]]["Default"]=isset($tmp_var["Default"])?$tmp_var["Default"]:"";
			$tbInfo[$tmp_var["Field"]]["Extra"]=isset($tmp_var["Extra"])?$tmp_var["Extra"]:"";
			$nbrChamps++;
		}
		$tbInfo["Number_Of_Fields"]=$nbrChamps;
		return $tbInfo;
	}
	function query($queryString) {
		if(!$this->connected) $this->connect();
		$this->precQueryId=@mysql_query($queryString,$this->refConnect);
		$this->errno=mysql_errno();
		$this->error=mysql_error();
		if(!$this->precQueryId) $this->erreur("la requête");
		else $this->queryExec++;
		return $this->precQueryId;		
	}
	function nextRecord($type=MYSQL_BOTH) {
		$this->enreg=@mysql_fetch_array($this->precQueryId,$type);
		$this->errno=mysql_errno();
		$this->error=mysql_error();
		return $this->enreg;
	}
	function erreur($message="") {
		echo "\n<P STYLE=\"color:#FF0000\">Attention : erreur lors de ".nl2br($message)."\nMySQL : $this->error.</P>";
	}
	function optimize($nom) {
		if(!$this->connected) $this->connect();
		$this->query("OPTIMIZE TABLE `$nom`");
	}
	function search($table,$champ,$chaine="",$tmp_var=array()) {
		$chaine=$chaine?$chaine:"*";
		$params=isset($tmp_var["PARAMS"])?" ".$tmp_var["PARAMS"]:"";
		$type=isset($tmp_var["TYPE"])?$tmp_var["TYPE"]:MYSQL_ASSOC;
		$chaine=ereg_replace("\*+","%",$chaine);
		$tmp_var=split("[ \t\n]+",trim($chaine));
		$chaine="";
		for($i=0;$i<count($tmp_var);$i++) {
			$like=$tmp_var[$i]{0}=="/"?"NOT LIKE":"LIKE";
			$tmp_var[$i]=$tmp_var[$i]{0}=="/"?substr($tmp_var[$i],1):$tmp_var[$i];
			if($i==count($tmp_var)-1) $chaine.="`$champ` $like '%$tmp_var[$i]%'";
			else $chaine.="`$champ` $like '%$tmp_var[$i]%' AND ";
		}
		unset($tmp_var);
		$chaine=ereg_replace("%+","%",$chaine);
		echo $chaine;
		$this->query("SELECT * FROM `".$table."` WHERE ".$chaine.$params);
		while($temp=$this->enregSuivant($type)) $tmp_var[]=$temp;
		return $tmp_var;
	}
}
?>

Conclusion :


Oui, je sais elle est à moitié anglaise, à moitié française, car à l'origine je l'avais fais entierement en francais mais après j'ai prefere la traduire ce que je n'ai pas fais entierement, dsl.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.