Advertisement
Guest User

wrapper

a guest
Jul 8th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Class dbWrapper {
  2. protected $_mysqli;
  3. protected $_debug;
  4.  
  5. public function __construct($host, $username, $password, $database, $debug) {
  6. $this->_mysqli = new mysqli($host, $username, $password, $database);
  7. $this->_debug = (bool) $debug;
  8. if (mysqli_connect_errno()) {
  9. if ($this->_debug) {
  10. echo mysqli_connect_error();
  11. debug_print_backtrace();
  12. }
  13. return false;
  14. }
  15. return true;
  16. }
  17.  
  18. public function q($query) {
  19. if ($query = $this->_mysqli->prepare($query)) {
  20. if (func_num_args() > 1) {
  21. $x = func_get_args();
  22. $args = array_merge(array(func_get_arg(1)),
  23. array_slice($x, 2));
  24. $args_ref = array();
  25. foreach($args as $k => &$arg) {
  26. $args_ref[$k] = &$arg;
  27. }
  28. call_user_func_array(array($query, 'bind_param'), $args_ref);
  29. }
  30. $query->execute();
  31.  
  32. if ($query->errno) {
  33. if ($this->_debug) {
  34. echo mysqli_error($this->_mysqli);
  35. debug_print_backtrace();
  36. }
  37. return false;
  38. }
  39.  
  40. if ($query->affected_rows > -1) {
  41. return $query->affected_rows;
  42. }
  43. $params = array();
  44. $meta = $query->result_metadata();
  45. while ($field = $meta->fetch_field()) {
  46. $params[] = &$row[$field->name];
  47. }
  48. call_user_func_array(array($query, 'bind_result'), $params);
  49.  
  50. $result = array();
  51. while ($query->fetch()) {
  52. $r = array();
  53. foreach ($row as $key => $val) {
  54. $r[$key] = $val;
  55. }
  56. $result[] = $r;
  57. }
  58. $query->close();
  59. return $result;
  60. } else {
  61. if ($this->_debug) {
  62. echo $this->_mysqli->error;
  63. debug_print_backtrace();
  64. }
  65. return false;
  66. }
  67. }
  68.  
  69. public function handle() {
  70. return $this->_mysqli;
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement