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
|
<?php
class Connexion{
private static $connectionString = "mysql:dbname=blog_POO;host=127.0.0.1;";
private static $connectionUser = "root";
private static $connectionPassword = "root";
private $connexion;
public function __construct(){
// mise en place de la connexion
$this->connexion = new PDO(self::$connectionString, self::$connectionUser, self::$connectionPassword);
//levée d'exception en cas d'erreur.
$this->connexion->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
}
public function endConnect(){
$this->connexion = null;
}
public function sql($sql,$array = null){
if (empty($this->connexion)){
echo "La connexion n'a pas été initialisée";
}
else{
//On prépapre la requête
$requ_Prepa = $this->connexion->prepare($sql);
$requ_Prepa->setFetchMode(PDO::FETCH_ASSOC);
//execution de la requête
$requ_Prepa->execute($array);
$datas = $requ_Prepa->fetchAll();
return $datas;
}
}
}
?> |
Partager