Advertisement
Guest User

Untitled

a guest
May 9th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Deux fonctions sont crées pour lancer une requéte SQL. Un exmple d'appel de ces fonctions dans du code :
  5. * - $tableauDeResultats = Connexion::query("SELECT * FROM nom_table");
  6. * Le résultat de la requète est enregistré dans la variable $tableauDeResultats
  7. * - $succes = Connexion::exec("INSERT..."); marche aussi pour UPDATE et DELETE
  8. * Le résultat de la requete est placé dans la variable $succes : si 0 alors la requète n'a pas
  9. * fonctionnée, sinon $succes contiendra le nombre d'enregistrement affectés
  10. */
  11.  
  12. class Connexion {
  13.  
  14. private static $_pdo = null;
  15.  
  16. private function __construct() {
  17.  
  18. }
  19.  
  20. private static function _get() {
  21. $dsn = 'mysql:dbname=coopenoix_v0;host=localhost';
  22. $user = 'root';
  23. $password = 'pwsio';
  24.  
  25. try {
  26. self::$_pdo = new PDO($dsn, $user, $password);
  27. } catch (PDOException $e) {
  28. echo 'Connexion échouée : ' . $e->getMessage();
  29. }
  30. self::$_pdo->exec('SET NAMES \'utf8\'');
  31. }
  32.  
  33. public static function query($query) {
  34. if (is_null(self::$_pdo)) {
  35. self::_get();
  36. }
  37.  
  38. $result = self::$_pdo->query($query);
  39. if (!$result) {
  40. throw new Exception('Erreur de requête : ' . $query);
  41. }
  42. return $result->fetchAll(PDO::FETCH_BOTH);
  43. }
  44.  
  45. public static function exec($query) {
  46. if (is_null(self::$_pdo)) {
  47. self::_get();
  48. }
  49. return self::$_pdo->exec($query);
  50. }
  51.  
  52. public static function getAll($table, $order = null) {
  53. $query = 'select * from `' . $table . '`';
  54. if (!is_null($order)) {
  55. $query .= ' order by ' . $order;
  56. }
  57. $itemsTemp = self::query($query);
  58. $items = [];
  59. foreach ($itemsTemp as $i) {
  60. $items[$i['id']] = $i;
  61. }
  62. return $items;
  63. }
  64.  
  65. public static function getOne($table, $id) {
  66. if (!self::estValideId($id)) {
  67. return null;
  68. }
  69. $query = 'select * from `' . $table . '` where id=' . $id;
  70. $result = self::query($query);
  71. if (isset($result[0])) {
  72. return $result[0];
  73. } else {
  74. return null;
  75. }
  76. }
  77.  
  78. public static function getTableauAssociatif($query) {
  79. $itemsTemp = self::query($query);
  80. $items = [];
  81. foreach ($itemsTemp as $i) {
  82. $items[$i[0]] = $i[1];
  83. }
  84. return $items;
  85. }
  86.  
  87. public static function estValideId($id) {
  88. return is_numeric($id) and $id > 0;
  89. }
  90.  
  91. public static function insert($table, $row) {
  92. if (isset($row['id'])) {
  93. unset($row['id']);
  94. }
  95. $query = 'insert into `' . $table . '`(' . implode(',', array_keys($row)) . ') values(';
  96. foreach ($row as $value) {
  97. if (is_null($value)) {
  98. $query .= 'null,';
  99. } else {
  100. $query .= '"' . $value . '",';
  101. }
  102. }
  103. $query = substr($query, 0, -1) . ')';
  104. return self::exec($query);
  105. }
  106.  
  107. public static function update($table, $row) {
  108. $query = 'update `' . $table . '` set ';
  109. $id = $row['id'];
  110. unset($row['id']);
  111. foreach ($row as $key => $value) {
  112. if (!is_numeric($key)) {
  113. if (is_null($value)) {
  114. $query .= $key . '=null,';
  115. } else {
  116. $query .= $key . '="' . $value . '",';
  117. }
  118. }
  119. }
  120. $query = substr($query, 0, -1);
  121. $query .= ' where id=' . $id;
  122. return self::exec($query);
  123. }
  124.  
  125. public static function delete($table, $id) {
  126. $query = 'delete from `' . $table . '` where id=' . $id;
  127. return self::exec($query);
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement