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
| <?php
class DB{
private $host = '';
private $username = '';
private $password = '';
private $database = '';
private $db;
public function __construct($host = null, $username = null, $password = null, $database = null){
if($host != null){
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
try{
$this->db = new PDO('mysql:host='.$this->host.';dbname='.$this->database.';charset=utf8',
$this->username,
$this->password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING
));
}catch(PDOException $e){
die('<h1>Impossible de se connecter a la base de donnee</h1>');
}
}
public function query($sql, $data = array()){
$req =$this->db->prepare($sql);
$req->execute($data);
return $req->fetchAll(PDO::FETCH_OBJ);
}
} |
Partager