Guest User

Untitled

a guest
Mar 4th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. <?php
  2.  
  3. class Db {
  4. private $host = 'localhost';
  5. private $db = 'bestdb';
  6. private $user = 'root';
  7. private $pass = '';
  8. private $charset = 'utf8';
  9. private $pdo;
  10.  
  11. function __construct() {
  12. try {
  13. $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
  14. $opt = array(
  15. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  16. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  17. );
  18. $this->pdo = new PDO($dsn, $this->user, $this->pass, $opt);
  19. } catch(PDOException $e) {
  20. echo 'Не удалось подключиться к бд: ' . $e->getMessage();
  21. }
  22. }
  23.  
  24. public function update($id, $arr) {
  25. try {
  26. $this->clearArr($arr);
  27. $sql = "UPDATE `users` SET " . $this->keyValueSet($arr, $values) . " WHERE `id` = ?";
  28. $values[] = $id;
  29. $smtp = $this->pdo->prepare($sql);
  30. $this->bindValue($smtp, $values);
  31. $smtp->execute();
  32. } catch(PDOException $e) {
  33. echo 'Не удалось обновить значение полей: ' . $e->getMessage();
  34. }
  35. }
  36.  
  37. public function insert($arr) {
  38. try {
  39. $this->clearArr($arr);
  40. $sql = "INSERT INTO `users` SET " . $this->keyValueSet($arr, $values);
  41. $smtp = $this->pdo->prepare($sql);
  42. $this->bindValue($smtp, $values);
  43. $smtp->execute();
  44. } catch(PDOException $e) {
  45. echo 'Не удалось вставить данные в бд: ' . $e->getMessage();
  46. }
  47. }
  48.  
  49. public function delete($id) {
  50. try {
  51. $this->clearString($id);
  52. $smtp = $this->pdo->prepare('DELETE FROM `users` WHERE `id` = ?');
  53. $smtp->bindValue(1, $id, PDO::PARAM_INT);
  54. $smtp->execute();
  55. } catch(PDOException $e) {
  56. echo 'Не удалось удалить строку из бд: ' . $e->getMessage();
  57. }
  58. }
  59.  
  60. public function select($arr = null, $offset = null, $limit = null) {
  61. if ($arr) {
  62. $this->clearArr($arr);
  63. $strWhere = substr(str_repeat('?, ', count($arr)), 0, -2);
  64. $sql = "SELECT * FROM `users` WHERE " . $this->keyValueSet($arr, $values, true);
  65. } elseif (!isset($arr)) {
  66. $sql = "SELECT * FROM `users`";
  67. }
  68.  
  69. if (isset($offset) && isset($limit)) {
  70. $sql .= " LIMIT ?, ?";
  71. $values[] = $offset;
  72. $values[] = $limit;
  73. }
  74. if (!isset($offset) && isset($limit)) {
  75. $sql .= " LIMIT ?";
  76. $values[] = $limit;
  77. }
  78.  
  79. try {
  80. $smtp = $this->pdo->prepare($sql);
  81. $this->bindValue($smtp, $values);
  82. $smtp->execute();
  83. } catch(PDOException $e) {
  84. echo 'Ошибка при поиске данных: ' . $e->getMessage();
  85. }
  86.  
  87. while ($row = $smtp->fetch()) {
  88. $result[] = $row;
  89. }
  90. return $result;
  91. }
  92.  
  93. private function bindValue(&$smtp, $values) {
  94. for ($i = 0; $i < count($values); $i++) {
  95. if (preg_match('/^[\d]*$/', $values[$i])) {
  96. $val = (int) $values[$i];
  97. $smtp->bindValue($i + 1, $val, PDO::PARAM_INT);
  98. } elseif (is_string($values[$i])) {
  99. $smtp->bindValue($i + 1, $values[$i], PDO::PARAM_STR);
  100. }
  101. }
  102. }
  103.  
  104. private function keyValueSet($keyVal, &$arrExecute, $string = false) {
  105. $result = '';
  106. foreach ($keyVal as $key => $val) {
  107. $result .= $string ? "`$key` = ? AND " : "`$key` = ?, ";
  108. $arrExecute[] = $val;
  109. }
  110. return $string ? substr($result, 0, -5) : substr($result, 0, -2);
  111. }
  112.  
  113. private function clearArr(&$arr) {
  114. $arr = array_map(function($el) {
  115. return strip_tags(trim($el));
  116. }, $arr);
  117. }
  118.  
  119. private function clearString(&$str) {
  120. $str = strip_tags(trim($str));
  121. }
  122.  
  123. }
Add Comment
Please, Sign In to add comment