par
labyelo » 02 févr. 2011, 00:31
Bonjour,
Je me crée une classe qui devrait me permettre de créer des requête plus facilement et j'aimerais savoir si ma méthode est correcte.
Voici mon code:
class DB
{
protected $select;
public function __toString()
{
$this->tostring = "<p><b><u>Cette classe permet la création de requêtes (SELECT, UPDATE, DELETE).</u></b></p>";
$this->tostring .= "<p>Utilisation.</p>";
$this->tostring .= "<li>SELECT</li>";
$this->tostring .= "<ul><li><b>db_select</b>(debug, champs, table, where, and, group, order) : appel la fonction</li>";
$this->tostring .= "<li><b>debug :</b> == false 'permet d'afficher la requête</li>";
$this->tostring .= "<li><b>champs :</b> indique les champs à utiliser</li>";
$this->tostring .= "<li><b>table :</b> indique la table à utiliser</li>";
$this->tostring .= "<li><b>where :</b> permet d'indiquer une condition</li>";
$this->tostring .= "<li><b>and :</b> permet d'indiquer une condition</li>";
$this->tostring .= "<li><b>group :</b>GROUP BY</li>";
$this->tostring .= "<li><b>order :</b>ORDER BY</li></ul>";
return $this->tostring;
}
public function db_select($debug, $champs, $table, $where, $and, $group, $order)
{
$this->table = $table; //- Choix de la table.
$this->champ = $champs; //- Choix des champs de la table.
$this->where = (( $where === true) ? NULL : $where ); //- En cas de clause WHERE.
$this->and = (( $and === true) ? NULL : $and ); //- En cas de clause AND.
$this->group = (( $group === true) ? NULL : $group ); //- En cas de GROUP BY.
$this->order = (( $order === true) ? NULL : $order ); //- En cas de ORDER BY.
$stmt = SPDO::getInstance()->prepare("SELECT
$this->champ
FROM
$this->table
$this->where
$this->and
$this->group
$this->order");
$stmt->execute();
$this->select = $stmt->fetchAll(PDO::FETCH_OBJ);
//************DEBUG**************//
//**Paramètre qui permet d'afficher la requête**//
$this->debug = (($debug === false) ? $stmt->debugDumpParams() : null );
echo $this->debug;
}
public function getSelect()
{
return $this->select;
}
}
Elle fonctionne correctement mais je ne suis pas certain que la méthode soit bonne
D'avance merci de vos remarques
@+

Bonjour,
Je me crée une classe qui devrait me permettre de créer des requête plus facilement et j'aimerais savoir si ma méthode est correcte.
Voici mon code:
[php]
class DB
{
protected $select;
public function __toString()
{
$this->tostring = "<p><b><u>Cette classe permet la création de requêtes (SELECT, UPDATE, DELETE).</u></b></p>";
$this->tostring .= "<p>Utilisation.</p>";
$this->tostring .= "<li>SELECT</li>";
$this->tostring .= "<ul><li><b>db_select</b>(debug, champs, table, where, and, group, order) : appel la fonction</li>";
$this->tostring .= "<li><b>debug :</b> == false 'permet d'afficher la requête</li>";
$this->tostring .= "<li><b>champs :</b> indique les champs à utiliser</li>";
$this->tostring .= "<li><b>table :</b> indique la table à utiliser</li>";
$this->tostring .= "<li><b>where :</b> permet d'indiquer une condition</li>";
$this->tostring .= "<li><b>and :</b> permet d'indiquer une condition</li>";
$this->tostring .= "<li><b>group :</b>GROUP BY</li>";
$this->tostring .= "<li><b>order :</b>ORDER BY</li></ul>";
return $this->tostring;
}
public function db_select($debug, $champs, $table, $where, $and, $group, $order)
{
$this->table = $table; //- Choix de la table.
$this->champ = $champs; //- Choix des champs de la table.
$this->where = (( $where === true) ? NULL : $where ); //- En cas de clause WHERE.
$this->and = (( $and === true) ? NULL : $and ); //- En cas de clause AND.
$this->group = (( $group === true) ? NULL : $group ); //- En cas de GROUP BY.
$this->order = (( $order === true) ? NULL : $order ); //- En cas de ORDER BY.
$stmt = SPDO::getInstance()->prepare("SELECT
$this->champ
FROM
$this->table
$this->where
$this->and
$this->group
$this->order");
$stmt->execute();
$this->select = $stmt->fetchAll(PDO::FETCH_OBJ);
//************DEBUG**************//
//**Paramètre qui permet d'afficher la requête**//
$this->debug = (($debug === false) ? $stmt->debugDumpParams() : null );
echo $this->debug;
}
public function getSelect()
{
return $this->select;
}
}
[/php]
Elle fonctionne correctement mais je ne suis pas certain que la méthode soit bonne
D'avance merci de vos remarques
@+ :)