Guest User

Untitled

a guest
Feb 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class db{
  2. private $db;
  3. function __construct($host, $db, $user, $password){
  4. $this->db = mysqli_connect($host, $db, $user, $password);
  5. }
  6.  
  7. function __destruct(){ mysqli_close($this->db); }
  8.  
  9. function query($query){
  10. $result = mysqli_query($this->db, $query) or die("error");
  11. return $result;
  12. }
  13. }
  14.  
  15. class data{
  16. private $db;
  17. function __construct(){
  18. if(empty($this->db)) $this->db = new db("127.0.0.1", "user", "password", "db_name");
  19. }
  20.  
  21. function __sleep(){
  22. // Отключаемся от бд
  23. $this->db->__destruct();
  24. return $this;
  25. }
  26.  
  27. // Метод вызывается перед unserialize()
  28. function __wakeup(){ self::__construct(); }
  29.  
  30. function get(){
  31. $query = "SELECT * FROM tbl_name";
  32. $db = $this->db->query($query);
  33. // Предположим что вернулась одна строка
  34. print_r(mysqli_fetch_row($db));
  35. }
  36. }
  37.  
  38. $obj1 = new obj();
  39. session_start();
  40. $_SESSION['obj'] = serialize($obj1);
  41.  
  42. $obj2 = unserialize($_SESSION['obj']);
  43. // Тут пишет ошибку
  44. $obj2->get();
Add Comment
Please, Sign In to add comment