Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- define('SYS', $_SERVER['DOCUMENT_ROOT'].'/system/');
- class DB
- {
- private $link;
- private $host = 'localhost';
- private $user = 'root';
- private $password = '';
- private $database = 'test5';
- private $table = 'users';
- // Подключаемся к базе
- public function __construct()
- {
- $this->link = mysqli_connect($this->host, $this->user, $this->password, $this->database);
- }
- // Делаем запрос к базе
- public function get($id, $field)
- {
- $query = $this->createSelect($id, $field);
- $result = $this->makeQuery($query);
- return $result[$field];
- }
- // Создаем строку с запросом
- private function createSelect($id, $field)
- {
- $table = $this->table;
- return "SELECT $field FROM $table WHERE id=$id";
- }
- // Совершаем запрос к базе
- private function makeQuery($query)
- {
- $result = mysqli_query($this->link, $query);
- return mysqli_fetch_assoc($result);
- }
- }
- class User
- {
- private $id;
- private $db;
- public function __construct($id)
- {
- $this->id = $id;
- // Создаем объект для работы с БД
- $this->db = new DB;
- }
- public function getName()
- {
- return $this->db->get($this->id, 'name');
- }
- public function getAge()
- {
- return $this->db->get($this->id, 'age');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement