/plugins/display_content/public/javascripts/jstree-v.pre1.0/_demo/_inc/class._database_i.php

https://gitlab.com/mes-participacao-digital/noosfero · PHP · 152 lines · 136 code · 15 blank · 1 comment · 25 complexity · d010df0b1cfe2e68615f427cbba44fbb MD5 · raw file

  1. <?php
  2. class _database {
  3. private $data = false;
  4. private $result = false;
  5. private $row = false;
  6. public $settings = array(
  7. "servername"=> "localhost",
  8. "serverport"=> "3306",
  9. "username" => false,
  10. "password" => false,
  11. "database" => false,
  12. "persist" => false,
  13. "dieonerror"=> false,
  14. "showerror" => false,
  15. "error_file"=> true
  16. );
  17. function __construct() {
  18. global $db_config;
  19. $this->settings = array_merge($this->settings, $db_config);
  20. if($this->settings["error_file"] === true) $this->settings["error_file"] = dirname(__FILE__)."/__mysql_errors.log";
  21. }
  22. function connect() {
  23. $this->data = new mysqli(
  24. $this->settings["servername"],
  25. $this->settings["username"],
  26. $this->settings["password"],
  27. $this->settings["database"],
  28. $this->settings["serverport"]
  29. );
  30. if(mysqli_connect_errno()) {
  31. $this->error("Connection error: ".mysqli_connect_error() );
  32. return false;
  33. }
  34. if(!$this->data->set_charset("utf8")) {
  35. $this->error("Error loading character set utf8");
  36. return false;
  37. }
  38. return true;
  39. }
  40. function query($sql) {
  41. if(!$this->data && !$this->connect()) {
  42. $this->error("Could node connect for query: ".$sql);
  43. return false;
  44. }
  45. //echo $sql."<br />:";
  46. if(!($this->result = $this->data->query($sql))) $this->error($sql);
  47. return ($this->result) ? true : false;
  48. }
  49. function nextr(){
  50. if(!$this->result) {
  51. $this->error("No query pending");
  52. return false;
  53. }
  54. unset($this->row);
  55. $this->row = $this->result->fetch_array(MYSQL_BOTH);
  56. return ($this->row) ? true : false ;
  57. }
  58. function get_row($mode = "both") {
  59. if(!$this->row) return false;
  60. $return = array();
  61. switch($mode) {
  62. case "assoc":
  63. foreach($this->row as $k => $v) {
  64. if(!is_int($k)) $return[$k] = $v;
  65. }
  66. break;
  67. case "num":
  68. foreach($this->row as $k => $v) {
  69. if(is_int($k)) $return[$k] = $v;
  70. }
  71. break;
  72. default:
  73. $return = $this->row;
  74. break;
  75. }
  76. return array_map("stripslashes",$return);
  77. }
  78. function get_all($mode = "both", $key = false) {
  79. if(!$this->result) {
  80. $this->error("No query pending");
  81. return false;
  82. }
  83. $return = array();
  84. while($this->nextr()) {
  85. if($key !== false) $return[$this->f($key)] = $this->get_row($mode);
  86. else $return[] = $this->get_row($mode);
  87. }
  88. return $return;
  89. }
  90. function f($index) {
  91. return stripslashes($this->row[$index]);
  92. }
  93. function go_to($row) {
  94. if(!$this->result) {
  95. $this->error("No query pending");
  96. return false;
  97. }
  98. if(!$this->data->data_seek($row)) $this->error();
  99. }
  100. function nf() {
  101. if (!$this->result) {
  102. $this->error("nf: no result set");
  103. return false;
  104. }
  105. return $this->result->num_rows;
  106. }
  107. function af() {
  108. return $this->data->affected_rows;
  109. }
  110. function error($string = "") {
  111. $error = $this->data->error;
  112. if($this->settings["show_error"]) echo $error;
  113. if($this->settings["error_file"] !== false) {
  114. $handle = @fopen($this->settings["error_file"], "a+");
  115. if($handle) {
  116. @fwrite($handle, "[".date("Y-m-d H:i:s")."] ".$string." <".$error.">\n");
  117. @fclose($handle);
  118. }
  119. }
  120. if($this->settings["dieonerror"]) {
  121. if(isset($this->result)) $this->result->free();
  122. @$this->data->close();
  123. die();
  124. }
  125. }
  126. function insert_id() {
  127. return $this->data->insert_id;
  128. }
  129. function escape($string) {
  130. if(!$this->data) return addslashes($string);
  131. return $this->data->escape_string($string);
  132. }
  133. function destroy() {
  134. if(isset($this->result)) $this->result->free();
  135. if($this->data) $this->data->close();
  136. }
  137. }