begin process at 2012 05 27 22:11:25
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Base de données

 > CLASSE POUR ACCES A UNE BDD MYSQL

CLASSE POUR ACCES A UNE BDD MYSQL


 Information sur la source

Note :
Aucune note
Catégorie :Base de données Niveau :Initié Date de création :26/03/2003 Date de mise à jour :26/03/2003 14:29:56 Vu :4 045

Auteur : ronanry

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

ceci est une classe que j'ai developpe avec un ami
pour pouvoir faire la requete pour accéder a une base de données mysql
tout le code est commenté en haut du fichier
SI VOUS AVEZ DES SOUCIS penser a me mailer :)

Source

  • class CRITERE
  • {
  • /************************************************************************************************************************************
  • *functions utilisables : * *
  • * CRITERE (string tableS) * toutes les tables qui vont être utilisées dans la requête *
  • * contrainte (string contrainte) * ajoute contrainte au tableau des contraintes *
  • * select (string "champ1,champ2,...") * retourne "select champ1,champ2 from table where contrainte... *
  • * insert ([string table]) * comme select mais insert (default=CRITERE[0]) *
  • * update ([string table]) * comme select mais update (default=CRITERE[0]) *
  • * delete ([string table]) * comme select mais delete (default=CRITERE[0]) *
  • * selectOrder (string "champ1,champ2,...") * comme select avec en plus orderBy *
  • * addUpdate (string "ident=valeur") * pour avoir "set $id_libelle=$id_value" dans la requete update *
  • * addInsert ("id_libelle","'".$id_value."'"); * pour avoir "set $id_libelle=$id_value" dans la requete insert *
  • * addOrderBy (string nom_champ[,string table[,"ASC"|"DESC"]])* ajoute table.nom_champ au tableau orderBy (default=CRITERE[0]) *
  • * addTable (string table) * ajoute une table au tableau des tables *
  • * setlimit (int limite,int offset) * définit un nombre limite de variables à afficher *
  • * free_all () * nettoie tout les tableaux (utiliser si 2 requetes successives) *
  • * free_all_tables () * nettoie tout les tableaux (utiliser si 2 requetes successives) *
  • *************************************************************************************************************************************
  • * TODO *
  • * les fonctions update,... de la meme maniere que select *
  • * implementer la fonction count ?????? *
  • ************************************************************************************************************************************/
  • var $sens="";
  • var $malimit = 0;
  • var $monoffset = 0;
  • var $monWhere = array();
  • var $monInsert = array();
  • var $mesTables = array();
  • var $monInsert2 = array();
  • var $monOrderBy = array();
  • var $monUpdate = array();
  • function CRITERE() {
  • $this->mesTables = func_get_args();
  • }
  • function contrainte($const)
  • {
  • $this->monWhere[]=$const;
  • }
  • function select($champ) {
  • $s = "SELECT $champ FROM " . $this->from();
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $s .= "\n" . $this->limit();
  • $s .=";";
  • return $s;
  • }
  • function insert($table=0)
  • {
  • if (!$table)
  • $table=$this->mesTables[0];
  • $s = "INSERT INTO $table (" . $this->into();
  • $s.= ") VALUES (" .$this->values();
  • $s.= ")";
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $s .=";";
  • return $s;
  • }
  • function update($table=0)
  • {
  • if (!$table)
  • $table=$this->mesTables[0];
  • $s ="UPDATE $table SET ".$this->set();
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $s .=";";
  • return $s;
  • }
  • function delete($table=0)
  • {
  • if (!$table)
  • $table=$this->mesTables[0];
  • $s ="DELETE FROM $table";
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $s .=";";
  • return $s;
  • }
  • function selectOrder($champ) {
  • $s = "SELECT $champ FROM " . $this->from();
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $o = $this->orderBy();
  • if ($o != "") {
  • $s .= " ORDER BY $o";
  • }
  • $s .= "\n" . $this->limit();
  • $s .=";";
  • return $s;
  • }
  • function addUpdate($a_updater) {
  • $this->monUpdate[] = "$a_updater";
  • }
  • function addInsert($id_libelle,$id_value) {
  • $this->monInsert[] = "$id_libelle";
  • $this->monInsert2[] = "$id_value";
  • // a virer pour compatibilite ancienne à la place faire un free_all avant d'utiliser insert
  • //avant de faire n'importe quoi d'ailleurs
  • $this->monWhere=array();
  • }
  • function addOrderBy($id_libelle,$table=0,$type=0) {
  • if (!$table)
  • $table=$this->mesTables[0];
  • $this->monOrderBy[] = "$table.$id_libelle $type";
  • }
  • function addTable($table) {
  • $table = chop($table);
  • $add_table = 1;
  • for ($i=0;$i<count($this->mesTables);$i++)
  • {
  • if (chop($this->mesTables[$i]) == chop($table))
  • {
  • $add_table = 0;
  • }
  • }
  • if ($add_table) {
  • $this->mesTables[] = chop($table);
  • }
  • return $add_table;
  • }
  • function setlimit($limit, $offset) {
  • $this->malimit = $limit;
  • $this->monoffset = $offset;
  • }
  • function free_all()
  • {
  • $this->monWhere=array();
  • $this->monInsert=array();
  • $this->monInsert2=array();
  • $this->monOrderBy=array();
  • $this->monUpdate=array();
  • $malimit = 0;
  • $monoffset = 0;
  • $sens="";
  • }
  • function free_all_tables()
  • {
  • $this->mesTables = array();
  • }
  • /* function count($select) {
  • global $connect,$default;
  • $d=$default->db;
  • $s = "SELECT $select FROM " . $this->from();
  • $w = $this->where();
  • if ($w != "" ) {
  • $s .= " WHERE " . $w;
  • }
  • $q = $d->query($s);
  • return pg_numrows($q);
  • }*/
  • //pour la creation de la requete
  • //formule a ne pas utiliser comme ca....
  • //pas bon le dedoublonnage
  • function from() { return implode(", ", $this->mesTables); }
  • function set() { return implode(", ", $this->monUpdate); }
  • function into() { return implode(", ", $this->monInsert); }
  • function values() { return implode(", ", $this->monInsert2); }
  • function orderBy() { return implode(", ", $this->monOrderBy); }
  • function limit() {
  • if ($this->malimit) {
  • $lim = "LIMIT " . $this->malimit;
  • if ($this->monoffset) {
  • $lim .= ", " . $this->monoffset;
  • }
  • }
  • return $lim;
  • }
  • function where() {
  • $w = $this->monWhere[0];
  • for($i=1; $i < count($this->monWhere); $i++) {
  • $w .= " AND " . $this->monWhere[$i];
  • }
  • return $w;
  • }
  • }
class CRITERE
{
/************************************************************************************************************************************
*functions utilisables :										*																	*
*	CRITERE		(string tableS)									*	toutes les tables qui vont être utilisées dans la requête		*
*	contrainte	(string contrainte)								*	ajoute contrainte au tableau des contraintes				 	*
*	select		(string "champ1,champ2,...")					*	retourne "select champ1,champ2 from table where contrainte...	*
*	insert		([string table])								*	comme select mais insert	(default=CRITERE[0])				*
*	update		([string table])								*	comme select mais update	(default=CRITERE[0])				*
*	delete		([string table])								*	comme select mais delete	(default=CRITERE[0])				*
*	selectOrder	(string "champ1,champ2,...")					*	comme select avec en plus orderBy 								*
*	addUpdate	(string "ident=valeur")							*	pour avoir "set $id_libelle=$id_value" dans la requete update	*
*	addInsert	("id_libelle","'".$id_value."'");				*	pour avoir "set $id_libelle=$id_value" dans la requete insert	*
*	addOrderBy	(string nom_champ[,string table[,"ASC"|"DESC"]])*	ajoute table.nom_champ au tableau orderBy (default=CRITERE[0])	*
*	addTable	(string table)									*	ajoute une table au tableau des tables							*
*	setlimit	(int limite,int offset)							*	définit un nombre limite de variables à afficher				*
*	free_all	()												*	nettoie tout les tableaux (utiliser si 2 requetes successives)	*
*	free_all_tables	()											*	nettoie tout les tableaux (utiliser si 2 requetes successives)	*
*************************************************************************************************************************************
*	TODO																															*
*			les fonctions update,... de la meme maniere que select																	*
*			implementer la fonction count	??????																					*
************************************************************************************************************************************/

	var $sens="";
	var $malimit = 0;
	var $monoffset = 0;
	var $monWhere = array();
	var $monInsert = array();
	var $mesTables = array();
	var $monInsert2 = array();
	var $monOrderBy = array();
	var $monUpdate = array();

	function CRITERE() {
	  $this->mesTables = func_get_args();
	}
	
	function contrainte($const)
	{
		$this->monWhere[]=$const;
	}
	
	function select($champ) {
		$s = "SELECT $champ FROM " . $this->from();
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$s .= "\n" . $this->limit();
		$s .=";";
		return $s;
	}

	function insert($table=0)
	{
		if (!$table)
			$table=$this->mesTables[0];
		$s = "INSERT INTO $table (" . $this->into();
		$s.= ") VALUES (" .$this->values();
		$s.= ")";
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$s .=";";
		return $s;
	}

	function update($table=0)
	{
		if (!$table)
			$table=$this->mesTables[0];
		$s ="UPDATE $table SET ".$this->set();
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$s .=";";
		return $s;
	}
	
	function delete($table=0)
	{
		if (!$table)
			$table=$this->mesTables[0];
		$s ="DELETE FROM $table";
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$s .=";";
		return $s;
	}

	function selectOrder($champ) {
		$s = "SELECT $champ FROM " . $this->from();
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$o = $this->orderBy();
		if ($o != "") {
		  $s .= " ORDER BY $o";
		}
		$s .= "\n" . $this->limit();
		$s .=";";		
		return $s;
	}

	function addUpdate($a_updater) {
	  $this->monUpdate[] = "$a_updater";
	}

	function addInsert($id_libelle,$id_value) {
	  $this->monInsert[] = "$id_libelle";
	  $this->monInsert2[] = "$id_value";
	 	// a virer pour compatibilite ancienne à la place faire un free_all avant d'utiliser insert
		//avant de faire n'importe quoi d'ailleurs
	  $this->monWhere=array(); 
	}

	function addOrderBy($id_libelle,$table=0,$type=0) {
		if (!$table)
			$table=$this->mesTables[0];
	  $this->monOrderBy[] = "$table.$id_libelle $type"; 
	}

	function addTable($table) {
		$table = chop($table);
	  	$add_table = 1;
	  	for ($i=0;$i<count($this->mesTables);$i++)
	  	{
	  		if (chop($this->mesTables[$i]) ==  chop($table))
			{
	    		$add_table = 0;
	    	}
	  	}
	  	if ($add_table) {
	  		$this->mesTables[] = chop($table);
	  	}
	  	return $add_table;
	}

	function setlimit($limit, $offset) {
	$this->malimit = $limit;
	$this->monoffset = $offset;
	}
	
	function free_all()
	{
		$this->monWhere=array();
		$this->monInsert=array();
		$this->monInsert2=array();
		$this->monOrderBy=array();
		$this->monUpdate=array();
		$malimit = 0;
		$monoffset = 0;
		$sens="";
	}
	function free_all_tables()
	{
  		$this->mesTables = array();
	}

/*	function count($select) {
		global $connect,$default;
		$d=$default->db;
		$s = "SELECT $select FROM " . $this->from();
		$w = $this->where();
		if ($w != "" ) {
		  $s .= " WHERE " . $w;
		}
		$q = $d->query($s);
		return pg_numrows($q);	
	}*/
	
	
	
//pour la creation de la requete
//formule a ne pas utiliser comme ca....
//pas bon le dedoublonnage
	function from()		{	return implode(", ", $this->mesTables);		}
	function set()		{	return implode(", ", $this->monUpdate);		}
	function into()		{	return implode(", ", $this->monInsert);		}
	function values()	{	return implode(", ", $this->monInsert2);	}
	function orderBy()	{	return implode(", ", $this->monOrderBy);	}
	function limit() {
	  if ($this->malimit) {
	    $lim = "LIMIT " . $this->malimit;
	    if ($this->monoffset) {
	      $lim .= ", " . $this->monoffset;
	    }
	  }
	  return $lim;
	}

	function where() {
		$w = $this->monWhere[0];
		for($i=1; $i < count($this->monWhere); $i++) {
		  $w .= " AND " . $this->monWhere[$i];
		}
		return $w;
	}
	

}



 Sources du même auteur

ENVOYER DES DONNÉES A FLASH
UPLOAD DE PLUSIEURS FICHIERS
Source avec Zip Source avec une capture MODIFIER UN FICHIER PNG POUR ECRIRE DESSUS
TABLEAU BICOLORE
AFFICHER LES ZEROS APRES LA VIRGULES

 Sources de la même categorie

Source avec Zip ORM : DAO, ACTIVERECORD ET DBLIST par Reldan
Source avec une capture CET EXTRAIT PERMET D'AJOUTER DANS UN TABLEAU UNE AGRÉGATION ... par Denis007
EXPORT DE BASE AU FORMAT CSV par remib74
Source avec Zip RECHERCHE DES DOUBLONS DANS UNE TABLE MYSQL EN SÉLECTIONNANT... par aladec2007
[CRON] INSERT ON DUPLICATE KEY UPDATE par pierreSabatier

Commentaires et avis

Commentaire de Kirua le 27/03/2003 07:57:22

pas de connexion ?
moi j'en ai faite une aussi, mais avec connexion.
elle est tt de même moins large que la tienne, quoique plus ciblée.
Par exemple, et je te recommande de l'implémenter, c assez simple, une méthode permet de récupérer la table sous forme de tableau. Donc en une ligne dans le code final !
bonne continuation

Commentaire de ronanry le 27/03/2003 09:52:27

je n'ai aps implementer la connexion pour la simple et bonne raison que j'ai une classe qui le fait deja :)
et elle aussi est plus large :)

Commentaire de Kirua le 28/03/2003 19:15:10

alors je ne dis rien ;)
de fait, les classes nous facilitent la vie !
perso, et je vous encourage tous à faire pareil, j'ai crée une bibliothèque de classes tout à fait adaptables (même la langue) pour livre d'or, gestion de mailing list, news, sondages, mysql, fichiers ini, page de téléchargements, etc...

l'avantage, c que vous réutilisez l'architecture pour vos sites, mais si vous centralisez les options dans un fichier ini lu par une fonction php, vous modifiez radicalement l'aspect de vos pages en peu d'effort !

apprenez la programmation objet, vous y gagnerez !

Commentaire de Dark_Genova le 15/04/2004 16:03:12

Je ne vois pas l'interet de la POO ... sachant qu'on peut faire pareil avec une sucession de fonction je ne vois pas ce qu'aportent les classes ;)
++

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,234 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales