Guest User

Untitled

a guest
Feb 4th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. <pre>
  2. <?php
  3. ini_set('display_errors',1);
  4. error_reporting(E_ALL);
  5.  
  6. class Database
  7. {
  8. protected $db_host = "host";
  9. protected $db_user = "user";
  10. protected $db_pass = "password";
  11. protected $db_name = "database";
  12. protected $connection = false;
  13.  
  14. protected $mysqli;
  15.  
  16. public function __construct()
  17. {
  18. if(!$this->connection)
  19. {
  20. $this->mysqli = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
  21. if($this->mysqli->connect_errno)
  22. {
  23. echo "Не удалось подключится к MySQL: " . $mysqli->connect_error;
  24. }
  25. else
  26. {
  27. if(!$this->mysqli->set_charset("utf8"))
  28. {
  29. printf("Ошибка при загрузке набора символов utf8: %s\n", $this->mysqli->error);
  30. return false;
  31. }
  32. else
  33. {
  34. printf("Текущий набор символов: %s\n", $this->mysqli->character_set_name());
  35. $this->connection = true;
  36. return true;
  37. }
  38. }
  39. }
  40. else
  41. {
  42. return $this->mysqli;
  43. return true;
  44. }
  45. }
  46.  
  47. public function tableExist($table)
  48. {
  49. $tablesInDatabase = $this->mysqli->query('SHOW TABLES FROM ' . $this->db_name . ' LIKE "' . $table . '"');
  50. if($tablesInDatabase)
  51. {
  52. if($tablesInDatabase->num_rows == 1)
  53. {
  54. echo "Существует таблица " . $table . " в базе данных";
  55. return true;
  56. }
  57. else
  58. {
  59. echo "Нет доступна к таблице " . $table;
  60. return false;
  61. }
  62. }
  63. }
  64.  
  65. public function setDatabase($name)
  66. {
  67. if(!$this->connection)
  68. {
  69. $this->mysqli;
  70. $this->connection = false;
  71. $this->db_name = $name;
  72. $this->tableExist($name);
  73. }
  74. }
  75.  
  76. }
  77.  
  78. $db = new Database;
  79. $db->setDatabase('calculator');
  80.  
  81. ?>
Add Comment
Please, Sign In to add comment