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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 && sizeof($array) != 4) {
  31. throw new Exception('wrong_arg_nmbr_where');
  32. }
  33. if(sizeof($array) == 3) {
  34. $whereClause .= $array[0]." ".$array[1]." ? AND ";
  35. array_push($arrayVerif, $array[2]);
  36. } else {
  37. $whereClause .= $array[0]." ".$array[1]." ".$array[2]." AND ";
  38. }
  39. }
  40. $request .= substr($whereClause, 0, -5);
  41. } else if(($upName = strtoupper($name)) == "ORDER BY") {
  42. if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
  43. throw new Exception('wrong_arg_nmbr_order_by');
  44. }
  45. $request .= " ".$upName." ".implode(' ', $value);
  46. } else if(($upName = strtoupper($name)) == "LIMIT") {
  47. if(sizeof($value) == 1) {
  48. // La colonne "limit" ne contient qu'un nombre de champs
  49. $request .= " $upName ".$value[0];
  50. } else if(sizeof($value) == 2) {
  51. // La colonne "limit" contient un index de départ et un nombre de champs
  52. $request .= " $upName ".$value[0].",".$value[1];
  53. } else {
  54. throw new Exception('wrong_arg_numbr_limit');
  55. }
  56. } else {
  57. throw new Exception('unknown_arg');
  58. }
  59. }
  60. $stmt = $this->bdd->prepare($request);
  61. if($stmt->execute($arrayVerif)) {
  62. return $stmt->fetchAll();
  63. } else {
  64. return null;
  65. }
  66. }
  67. function Insert($table, $values) {
  68. $request = "INSERT INTO $table(";
  69. $valeurs = "VALUES(";
  70. $arrayVerif = array();
  71. foreach($values as $name=>$value) {
  72. $request .= $name.",";
  73. $valeurs .= "?,";
  74. array_push($arrayVerif, $value);
  75. }
  76. $request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
  77. $stmt = $this->bdd->prepare($request);
  78. $stmt->execute($arrayVerif);
  79. }
  80. function Update($table, $update) {
  81. $request = "UPDATE $table SET ";
  82. $arrayVerif = array();
  83. foreach($update['set'] as $name=>$value) {
  84. $request .= $name."=?,";
  85. array_push($arrayVerif, $value);
  86. }
  87. $request = substr($request, 0, -1)." WHERE ";
  88. foreach($update['where'] as $value) {
  89. $request .= $value[0].$value[1]."? AND ";
  90. array_push($arrayVerif, $value[2]);
  91. }
  92. $request = substr($request, 0, -5);
  93. $stmt = $this->bdd->prepare($request);
  94. $stmt->execute($arrayVerif);
  95. }
  96. function beginTransaction() {
  97. $this->bdd->beginTransaction();
  98. }
  99. function commit() {
  100. $this->bdd->commit();
  101. }
  102. }