Advertisement
abbest

annonce.class.php

Mar 28th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2. // CRUD de la table SQL personnes
  3. class Annonce {
  4.     public $id, $ville, $categorie, $titre, $contenu;
  5.     //attributs de classe qui correspondent aux champs de la table
  6.     private $host,$dbname,$user,$password, $cnx;//paramètres de connexion
  7.     public function __construct($serveur="localhost",$bdd="monsite",$identifiant="root",$mdp=""){
  8.         $this->host =$serveur;
  9.         $this->dbname =$bdd;
  10.         $this->user =$identifiant;
  11.         $this->password =$mdp;
  12.         try
  13.         {
  14.             $this->cnx =new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->user,$this->password);
  15.         }
  16.         catch (Exception $e){
  17.             echo 'Code de l\'Erreur  : '.$e->getCode();
  18.             echo 'Message d\'Erreur  : '.$e->getMessage();
  19.         }
  20.     }
  21.     public function saveAnnonce(){ //add
  22.     $sql="INSERT INTO annonces(id, ville, categorie, titre, contenu)
  23.     values('','$this->ville','$this->categorie','$this->titre','$this->contenu')"; 
  24.     $res=$this->cnx->exec($sql);
  25.     if($res) return true;
  26.     else return false;
  27.         }
  28.     public function getAnnonce(){ //affiche
  29.     $sql="Select * from annonces"; 
  30.     $res=$this->cnx->query($sql);//INEXPLOITABLE
  31.     $data=$res->fetchAll(PDO::FETCH_ASSOC);
  32.     return $data;
  33.         }
  34.     public function searchAnnonce(){ //cherche
  35.     $sql="Select * from annonces WHERE (ville='$this->ville')&&(categorie='$this->categorie')";
  36.     $res=$this->cnx->query($sql);//INEXPLOITABLE
  37.     $data=$res->fetchAll(PDO::FETCH_ASSOC);
  38.     return $data;
  39.         }
  40.    
  41.     public function deleteAnnonce(){
  42.     $sql="Delete from annonces WHERE categorie='$this->categorie'";
  43.     print $sql;
  44.     $resultat=$this->cnx->exec($sql);//$resultat contient le nombre des lignes affectées
  45.     if($resultat) return true;
  46.         else return false;
  47.     }
  48.    
  49. }
  50.  
  51. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement