Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * DB Class
  5. */
  6. class DB
  7. {
  8. private static $_instance;
  9. private $_connection;
  10. private $_dbhost = DB_HOST;
  11. private $_dbuser = DB_USER;
  12. private $_dbpass = DB_PASS;
  13. private $_dbname = DB_NAME;
  14. private $_dbcharset = DB_CHARSET;
  15.  
  16. public static function getInstance()
  17. {
  18. if (!self::$_instance) {
  19. self::$_instance = new self();
  20. }
  21. return self::$_instance;
  22. }
  23.  
  24. private function __construct()
  25. {
  26. $this->_connection = new mysqli($this->_dbhost, $this->_dbuser, $this->_dbpass, $this->_dbname);
  27.  
  28. if (mysqli_connect_errno()) {
  29. trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
  30. }
  31.  
  32. $this->_connection->set_charset($this->_dbcharset);
  33. }
  34.  
  35. function getConnection()
  36. {
  37. return $this->_connection;
  38. }
  39.  
  40. function query($sql)
  41. {
  42. $args = func_get_args();
  43. $sql = array_shift($args);
  44. $link = $this->_connection;
  45.  
  46. $args = array_map(function ($param) use ($link) {
  47. return $link->escape_string($param);
  48. }, $args);
  49.  
  50. $sql = str_replace(array('%', '?'), array('%%', '%s'), $sql);
  51.  
  52. array_unshift($args, $sql);
  53.  
  54. $sql = call_user_func_array('sprintf', $args);
  55.  
  56. $this->_query = $this->_connection->query($sql);
  57. if($this->_query === false) throw new Exception('Database error: ' . $this->_connection->error);
  58.  
  59. return $this;
  60. }
  61.  
  62. function changeDb($db_name)
  63. {
  64. $this->_connection->select_db($db_name);
  65. }
  66.  
  67. function data_seek($offset = 0)
  68. {
  69. return $this->_query->data_seek($offset);
  70. }
  71.  
  72. function field_seek($field)
  73. {
  74. return $this->_query->field_seek($field);
  75. }
  76.  
  77. function insert_id()
  78. {
  79. return $this->_query->insert_id;
  80. }
  81.  
  82. function fetch_field_direct($field)
  83. {
  84. return $this->_query->fetch_field_direct($field);
  85. }
  86.  
  87. function assoc()
  88. {
  89. return $this->_query->fetch_assoc();
  90. }
  91.  
  92. function fetch_field()
  93. {
  94. while ($row = $this->_query->fetch_field()) $_result[] = $row->name;
  95. return $_result;
  96. }
  97.  
  98. function fetch_row()
  99. {
  100. return $this->_query->fetch_row();
  101. }
  102.  
  103. function fetch_object($class_name = "stdClass", $param = null)
  104. {
  105. if(is_null($param))
  106. {
  107. return $this->_query->fetch_object($class_name);
  108. }
  109. else
  110. {
  111. return $this->_query->fetch_object($class_name, $param);
  112. }
  113. }
  114.  
  115. function all()
  116. {
  117. while($row = $this->_query->fetch_assoc()) $_result[] = $row;
  118. return $_result;
  119. }
  120.  
  121. function __destruct()
  122. {
  123. $this->_connection->close();
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement