Guest User

Untitled

a guest
Feb 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2. class Core_Db_Mysql
  3. {
  4. private $queries = array();
  5. private $link = null;
  6.  
  7. public function connect($settings)
  8. {
  9. $this->link = new mysqli($settings['server'], $settings['username'], $settings['password'], $settings['database']);
  10. if($this->link->connect_error) {
  11. return false;
  12. }
  13.  
  14. return true;
  15. }
  16.  
  17. public function query($query)
  18. {
  19. $this->queries[] = $query;
  20. return @$this->link->query($query);
  21. }
  22.  
  23. public function quote($string)
  24. {
  25. return $this->link->real_escape_string($string);
  26. }
  27.  
  28. public function insert_query($table, $values)
  29. {
  30. $fields = implode('`,`', array_keys($values));
  31. $values = implode("','", array_map(array($this, 'quote'), $values));
  32. $query = "INSERT INTO ".$table." (`".$fields."`) VALUES ('".$values."')";
  33. if($this->query($query)) {
  34. return $this->insert_id();
  35. }
  36. else {
  37. return false;
  38. }
  39. }
  40.  
  41. public function replace_query($table, $values)
  42. {
  43. $fields = array();
  44. foreach($values as $k => $v) {
  45. $fields[] = "`".$k."`='".$this->quote($v)."'";
  46. }
  47. $query = "UPDATE ".$table." SET ".$fields;
  48. return $this->query($query);
  49. }
  50.  
  51. public function update_query($table, $values, $where = '')
  52. {
  53. $fields = array();
  54. foreach($values as $k => $v) {
  55. $fields[] = "`".$k."`='".$this->quote($v)."'";
  56. }
  57. $fields = implode(',', $fields);
  58. if($where) {
  59. $fields .= ' WHERE '.$where;
  60. }
  61. $query = "REPLACE INTO ".$table." SET ".$fields;
  62. return $this->query($query);
  63. }
  64.  
  65. public function start_transaction()
  66. {
  67. $this->query('START TRANSACTION');
  68. }
  69.  
  70. public function commit()
  71. {
  72. $this->query('COMMIT');
  73. }
  74.  
  75. public function rollback()
  76. {
  77. $query->query('ROLLBACK');
  78. }
  79.  
  80. public function delete_query($table, $where = '')
  81. {
  82. $query = 'DELETE FROM '.$table;
  83. if($where != '') {
  84. $query .= ' WHERE '.$where;
  85. }
  86. return $this->query($query);
  87. }
  88.  
  89. public function insert_id()
  90. {
  91. return $this->link->insert_id;
  92. }
  93.  
  94. public function affected_rows()
  95. {
  96. return $this->link->affected_rows;
  97. }
  98. }
Add Comment
Please, Sign In to add comment