SuperMonkey

messages.php

Jul 2nd, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2.  
  3.     try {
  4.         $bdd = new PDO("mysql:host=localhost;dbname=chat", "root", "************");
  5.     }
  6.    
  7.     catch(Exception $e) {
  8.         die($e->getMessage());
  9.     }
  10.  
  11.     if(isset($_GET["count"])) { //on veut juste l'id du dernier message
  12.         $req = $bdd->query("SELECT id FROM messages ORDER BY id DESC LIMIT 1");
  13.         $data = $req->fetch();
  14.         print $data["id"];
  15.         $req->closeCursor();
  16.     }
  17.    
  18.     elseif(isset($_GET["depuis"]) AND is_numeric($_GET["depuis"])) { //on affiche les messages ayant un id superieur aux anciens vus
  19.         $req = $bdd->prepare("SELECT * FROM messages WHERE id > ?");
  20.         $req->execute(array($_GET["depuis"]));
  21.        
  22.         while($data = $req->fetch()) {
  23.             print '<p class="message"><strong>'.htmlspecialchars($data["auteur"]).'</strong> : '.htmlspecialchars($data["post"])."</p>";
  24.         }
  25.        
  26.         $req->closeCursor();
  27.     }
  28.    
  29.     else {
  30.         $req = $bdd->query("SELECT * FROM messages");
  31.         while($data = $req->fetch()) {
  32.             print '<p class="message"><strong>'.htmlspecialchars($data["auteur"]).'</strong> : '.htmlspecialchars($data["post"])."</p>";
  33.         }
  34.         $req->closeCursor();
  35.     }
  36.    
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment