Guest User

Untitled

a guest
Feb 7th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. <?php
  2. class db {
  3.  
  4. protected $connection;
  5. protected $query;
  6. public $query_count = 0;
  7.  
  8. public function __construct($dbhost = 'localhost', $dbuser = 'root', $dbpass = '', $dbname = '', $charset = 'utf8') {
  9. $this->connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  10. if ($this->connection->connect_error) {
  11. die('Failed to connect to MySQL - ' . $this->connection->connect_error);
  12. }
  13. $this->connection->set_charset($charset);
  14. }
  15.  
  16. public function query($query) {
  17. if ($this->query = $this->connection->prepare($query)) {
  18. if (func_num_args() > 1) {
  19. $x = func_get_args();
  20. $args = array_slice($x, 1);
  21. $types = '';
  22. $args_ref = array();
  23. foreach ($args as $k => &$arg) {
  24. if (is_array($args[$k])) {
  25. foreach ($args[$k] as $j => &$a) {
  26. $types .= $this->_gettype($args[$k][$j]);
  27. $args_ref[] = &$a;
  28. }
  29. } else {
  30. $types .= $this->_gettype($args[$k]);
  31. $args_ref[] = &$arg;
  32. }
  33. }
  34. array_unshift($args_ref, $types);
  35. call_user_func_array(array($this->query, 'bind_param'), $args_ref);
  36. }
  37. $this->query->execute();
  38. if ($this->query->errno) {
  39. die('Unable to process MySQL query (check your params) - ' . $this->query->error);
  40. }
  41. $this->query_count++;
  42. } else {
  43. die('Unable to prepare statement (check your syntax) - ' . $this->query->error);
  44. }
  45. return $this;
  46. }
  47.  
  48. public function fetchAll() {
  49. $params = array();
  50. $meta = $this->query->result_metadata();
  51. while ($field = $meta->fetch_field()) {
  52. $params[] = &$row[$field->name];
  53. }
  54. call_user_func_array(array($this->query, 'bind_result'), $params);
  55. $result = array();
  56. while ($this->query->fetch()) {
  57. $r = array();
  58. foreach ($row as $key => $val) {
  59. $r[$key] = $val;
  60. }
  61. $result[] = $r;
  62. }
  63. $this->query->close();
  64. return $result;
  65. }
  66.  
  67. public function fetchArray() {
  68. $params = array();
  69. $meta = $this->query->result_metadata();
  70. while ($field = $meta->fetch_field()) {
  71. $params[] = &$row[$field->name];
  72. }
  73. call_user_func_array(array($this->query, 'bind_result'), $params);
  74. $result = array();
  75. while ($this->query->fetch()) {
  76. foreach ($row as $key => $val) {
  77. $result[$key] = $val;
  78. }
  79. }
  80. $this->query->close();
  81. return $result;
  82. }
  83.  
  84. public function numRows() {
  85. $this->query->store_result();
  86. return $this->query->num_rows;
  87. }
  88.  
  89. public function close() {
  90. return $this->connection->close();
  91. }
  92.  
  93. public function affectedRows() {
  94. return $this->query->affected_rows;
  95. }
  96.  
  97. private function _gettype($var) {
  98. if(is_string($var)) return 's';
  99. if(is_float($var)) return 'd';
  100. if(is_int($var)) return 'i';
  101. return 'b';
  102. }
  103.  
  104. }
  105. ?>
  106.  
  107. public function __construct($db) {
  108. $this->db = $db;
  109. }
Add Comment
Please, Sign In to add comment