Advertisement
Guest User

Untitled

a guest
May 12th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. define('SYS', $_SERVER['DOCUMENT_ROOT'].'/system/');
  5.  
  6. class DB
  7. {
  8. private $link;
  9. private $host = 'localhost';
  10. private $user = 'root';
  11. private $password = '';
  12. private $database = 'test5';
  13. private $table = 'users';
  14.  
  15. // Подключаемся к базе
  16. public function __construct()
  17. {
  18. $this->link = mysqli_connect($this->host, $this->user, $this->password, $this->database);
  19. }
  20.  
  21. // Делаем запрос к базе
  22. public function get($id, $field)
  23. {
  24. $query = $this->createSelect($id, $field);
  25. $result = $this->makeQuery($query);
  26. return $result[$field];
  27. }
  28.  
  29. // Создаем строку с запросом
  30. private function createSelect($id, $field)
  31. {
  32. $table = $this->table;
  33. return "SELECT $field FROM $table WHERE id=$id";
  34. }
  35.  
  36. // Совершаем запрос к базе
  37. private function makeQuery($query)
  38. {
  39. $result = mysqli_query($this->link, $query);
  40. return mysqli_fetch_assoc($result);
  41. }
  42. }
  43.  
  44. class User
  45. {
  46. private $id;
  47. private $db;
  48.  
  49. public function __construct($id)
  50. {
  51. $this->id = $id;
  52.  
  53. // Создаем объект для работы с БД
  54. $this->db = new DB;
  55. }
  56.  
  57. public function getName()
  58. {
  59. return $this->db->get($this->id, 'name');
  60. }
  61.  
  62. public function getAge()
  63. {
  64. return $this->db->get($this->id, 'age');
  65. }
  66.  
  67. }
  68. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement