Classe pour acces a une bdd mysql

Contenu du snippet

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 / Exemple :


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; } }

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.