<?php
/**
*
*/
class Model {
private $dsn ;
private $user ;
private $pass ;
public function __construct($dsn, $user, $pass){
$this->dsn = $dsn ;
$this->user = $user ;
$this->pass = $pass ;
try{
$this->db = new PDO($dsn, $user, $pass);
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'Erreur : ' . $e->getMessage();
}
}
public function lire(){
$sql = "SELECT * FROM users";
$res = $this->db->query($sql);
while($row = $res->fetchAll(PDO::FETCH_OBJ)){
return $row ;
}
}
public function ajouter(){
try{
$sql = $this->db->prepare("INSERT INTO users (nom, prenom, age, email) VALUE (:nom, :prenom, :age, :email)");
$sql->bindParam(1, $nom);
$sql->bindParam(2, $prenom);
$sql->bindParam(3, $age);
$sql->bindParam(4, $email);
$nom = "little";
$prenom = "john";
$age = 27;
$email = "john@gmail.ma";
$data = array('nom' => 'little','prenom' => 'john','age' => 27,'email' => 'john@gmail.ma');
$sql->execute($data);
}catch(PDOException $e){
echo $e->getMessage();
}
}
public function supprimer($id){
$sql = "DELETE FROM users WHERE id = {$id}";
$this->db->exec($sql);
}
public function modifier($id){
try{
$sql = $this->db->prepare("UPDATE users SET nom = :nom, prenom = :prenom, age = :age, email = :email WHERE id = {$id}");
$sql->bindParam(1, $nom);
$sql->bindParam(2, $prenom);
$sql->bindParam(3, $age);
$sql->bindParam(4, $email);
$nom = "Hunter";
$prenom = "bad";
$age = 27;
$email = "bad@gmail.ma";
$data = array('nom' => 'Hunter','prenom' => 'bad','age' => 27,'email' => 'bad@gmail.ma');
$sql->execute($data);
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
?>
Partager