Créé dans le cadre du projet de fin d'année de la promo 2018 de CIR2 de l'ISEN Brest/Rennes, le Burger Quizz est une adaptation numérique du jeu télévisé éponyme, plus précisément d'une épreuve spécifique de ce jeu : le "Sel ou Poivre".

class.connector.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class Connector {
  3. private $bdd;
  4. function __construct() {
  5. $host = "localhost";
  6. $db = "burgerquizz";
  7. $user = "alain";
  8. $pass = "chabat";
  9. $this->bdd = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
  10. $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. }
  12. /*
  13. Exemple de $options :
  14. $options = array(
  15. "where" => array(
  16. array("foo", "=", "bar"),
  17. array("blbl", ">", 5)
  18. ),
  19. "order by" => array("foo", "desc"),
  20. "limit" => array(0, 10) // Ou array(10)
  21. );
  22. */
  23. function Select($fields, $tables, $options = array()) {
  24. $request = "SELECT $fields FROM $tables ";
  25. $arrayVerif = array();
  26. foreach($options as $name=>$value) {
  27. if(($upName = strtoupper($name)) == "WHERE") {
  28. $whereClause = " $upName ";
  29. foreach($value as $array) {
  30. if(sizeof($array) != 3) {
  31. throw new Exception('wrong_arg_nmbr_where');
  32. }
  33. $whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
  34. $arrayVerif[":".$array[0]] = $array[2];
  35. }
  36. $request .= substr($whereClause, 0, -5);
  37. } else if(($upName = strtoupper($name)) == "ORDER BY") {
  38. if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
  39. throw new Exception('wrong_arg_nmbr_order_by');
  40. }
  41. $request .= " ".$upName." ".implode(' ', $value);
  42. } else if(($upName = strtoupper($name)) == "LIMIT") {
  43. if(sizeof($value) == 1) {
  44. // La colonne "limit" ne contient qu'un nombre de champs
  45. $request .= " $upName ".$value[0];
  46. } else if(sizeof($value) == 2) {
  47. // La colonne "limit" contient un index de départ et un nombre de champs
  48. $request .= " $upName ".$value[0].",".$value[1];
  49. } else {
  50. throw new Exception('wrong_arg_numbr_limit');
  51. }
  52. } else {
  53. throw new Exception('unknown_arg');
  54. }
  55. }
  56. $stmt = $this->bdd->prepare($request);
  57. if($stmt->execute($arrayVerif)) {
  58. return $stmt->fetchAll();
  59. } else {
  60. return null;
  61. }
  62. }
  63. function Insert($table, $values) {
  64. $request = "INSERT INTO $table(";
  65. $valeurs = "VALUES(";
  66. $arrayVerif = array();
  67. foreach($values as $name=>$value) {
  68. $request .= $name.",";
  69. $valeurs .= "?,";
  70. array_push($arrayVerif, $value);
  71. }
  72. $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
  73. $stmt = $this->bdd->prepare($request);
  74. $stmt->execute($arrayVerif);
  75. }
  76. function Update($table, $update) {
  77. $request = "UPDATE $table SET ";
  78. $arrayVerif = array();
  79. foreach($update['set'] as $name=>$value) {
  80. $request .= $name."=?,";
  81. array_push($arrayVerif, $value);
  82. }
  83. $request = substr($request, 0, -1)." WHERE ";
  84. foreach($update['where'] as $value) {
  85. $request .= $value[0].$value[1]."? AND ";
  86. array_push($arrayVerif, $value[2]);
  87. }
  88. $request = substr($request, 0, -5);
  89. $stmt = $this->bdd->prepare($request);
  90. $stmt->execute($arrayVerif);
  91. }
  92. function beginTransaction() {
  93. $this->bdd->beginTransaction();
  94. }
  95. function commit() {
  96. $this->bdd->commit();
  97. }
  98. }