1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
$db = new Mysql();
// sélection d'une liste
$listesPages = $db->returnListe(
array(
"table" => pages,
"fields" => 'id,titre',
"condition" => ' tpl=:tpl ',
"varCondition" => 2
)
);
class Mysql extends Exception {
protected static $conx;
/*
* fonction qui se connecte à un serveur et une db Mysql
* @host : serveur
* @login : login de connexion
* @password : mot de passe de connexion
* $db : nom de la base
*/
public static function connectDb(){
if(!isset($conx) || (empty($conx))){
try {
$conx = new PDO("mysql:host=".DB_HOST.";dbname=".DB."", DB_LOGIN, DB_PASSWORD);
$conx->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); // les noms de champs seront en caractères minuscules
$conx->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION); // les erreurs lanceront des exceptions
}
catch (PDOException $e){
echo "Connection à la base de donnée impossible" .
"<br/>" . $e->getMessage().
"<br/> LINE : " . $e->getLine();
}
}
return $conx;
}
/*
* Fonction qui sélectionne une liste
* @arrayReq : tableau pour construire une requête
* @arrayReq['table'] = table à sélectionnée
* @arrayReq['fiels'] = champs à sélectionner, tous les champs si vide
* @arrayReq['condition'] = condition après le where sans le WHERE
*/
public function returnListe($arrayReq){
$conx = $this->connectDb();
$table = $arrayReq['table'];
$fields = (!isset($arrayReq['fields'])) ? '*' : $arrayReq['fields'];
if(isset($arrayReq['condition'])){
$condition = 'WHERE' . $arrayReq['condition'];
$varCondition = $arrayReq['varCondition'];
}
$limit = (!isset($arrayReq['limit'])) ? '' : ' LIMIT '.$arrayReq['limit'];
$order = (!isset($arrayReq['order'])) ? '' : ' ORDER BY '.$arrayReq['order'];
$requete = $conx->prepare('SELECT '.$fields.' FROM '.$table.' '.$condition.' '.$order.' '.$limit.' ');
if($requete->execute(array("tpl"=>$varCondition))){
$return = $requete->fetchAll();
return $return;
$conx = null;
}else{
throw new Exception("Requête impossible", $cnx->errorInfo());
}
}
/*
* Fonction qui sélectionne une liste
* @arrayReq : tableau pour construire une requête
* @arrayReq['table'] = table à sélectionnée
* @arrayReq['fiels'] = champs à sélectionner, tous les champs si vide
* @arrayReq['condition'] = condition après le where sans le WHERE
*/
public function returnUnique($arrayReq){
$conx = $this->connectDb();
$table = $arrayReq['table'];
$fields = (!isset($arrayReq['fiels'])) ? '*' : $arrayReq['fiels'];
if(isset($arrayReq['condition'])){
$condition = 'WHERE' . $arrayReq['condition'];
$varCondition = $arrayReq['varCondition'];
}
$requete = $conx->prepare('SELECT id,titre FROM '.$table.' '.$condition.' ');
if($requete->execute(array("id"=>$varCondition))){
$return = $requete->fetch();
return $return;
}else{
throw new Exception("Requête impossible", $cnx->errorInfo());
}
}
// Sets
// gets
} |
Partager