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

https://gitlab.com/noosfero-mes/noosfero-mes · PHP · 146 lines · 132 code · 14 blank · 0 comment · 26 complexity · d156d1a9970b952c4d4581b4149ff7e7 MD5 · raw file

  1. <?php
  2. class _database {
  3. private $link = 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. if (!$this->link) {
  24. $this->link = ($this->settings["persist"]) ?
  25. mysql_pconnect(
  26. $this->settings["servername"].":".$this->settings["serverport"],
  27. $this->settings["username"],
  28. $this->settings["password"]
  29. ) :
  30. mysql_connect(
  31. $this->settings["servername"].":".$this->settings["serverport"],
  32. $this->settings["username"],
  33. $this->settings["password"]
  34. ) or $this->error();
  35. }
  36. if (!mysql_select_db($this->settings["database"], $this->link)) $this->error();
  37. if($this->link) mysql_query("SET NAMES 'utf8'");
  38. return ($this->link) ? true : false;
  39. }
  40. function query($sql) {
  41. if (!$this->link && !$this->connect()) $this->error();
  42. if (!($this->result = mysql_query($sql, $this->link))) $this->error($sql);
  43. return ($this->result) ? true : false;
  44. }
  45. function nextr() {
  46. if(!$this->result) {
  47. $this->error("No query pending");
  48. return false;
  49. }
  50. unset($this->row);
  51. $this->row = mysql_fetch_array($this->result, MYSQL_BOTH);
  52. return ($this->row) ? true : false ;
  53. }
  54. function get_row($mode = "both") {
  55. if(!$this->row) return false;
  56. $return = array();
  57. switch($mode) {
  58. case "assoc":
  59. foreach($this->row as $k => $v) {
  60. if(!is_int($k)) $return[$k] = $v;
  61. }
  62. break;
  63. case "num":
  64. foreach($this->row as $k => $v) {
  65. if(is_int($k)) $return[$k] = $v;
  66. }
  67. break;
  68. default:
  69. $return = $this->row;
  70. break;
  71. }
  72. return array_map("stripslashes",$return);
  73. }
  74. function get_all($mode = "both", $key = false) {
  75. if(!$this->result) {
  76. $this->error("No query pending");
  77. return false;
  78. }
  79. $return = array();
  80. while($this->nextr()) {
  81. if($key !== false) $return[$this->f($key)] = $this->get_row($mode);
  82. else $return[] = $this->get_row($mode);
  83. }
  84. return $return;
  85. }
  86. function f($index) {
  87. return stripslashes($this->row[$index]);
  88. }
  89. function go_to($row) {
  90. if(!$this->result) {
  91. $this->error("No query pending");
  92. return false;
  93. }
  94. if(!mysql_data_seek($this->result, $row)) $this->error();
  95. }
  96. function nf() {
  97. if ($numb = mysql_num_rows($this->result) === false) $this->error();
  98. return mysql_num_rows($this->result);
  99. }
  100. function af() {
  101. return mysql_affected_rows();
  102. }
  103. function error($string="") {
  104. $error = mysql_error();
  105. if($this->settings["show_error"]) echo $error;
  106. if($this->settings["error_file"] !== false) {
  107. $handle = @fopen($this->settings["error_file"], "a+");
  108. if($handle) {
  109. @fwrite($handle, "[".date("Y-m-d H:i:s")."] ".$string." <".$error.">\n");
  110. @fclose($handle);
  111. }
  112. }
  113. if($this->settings["dieonerror"]) {
  114. if(isset($this->result)) mysql_free_result($this->result);
  115. mysql_close($this->link);
  116. die();
  117. }
  118. }
  119. function insert_id() {
  120. if(!$this->link) return false;
  121. return mysql_insert_id();
  122. }
  123. function escape($string){
  124. if(!$this->link) return addslashes($string);
  125. return mysql_real_escape_string($string);
  126. }
  127. function destroy(){
  128. if (isset($this->result)) mysql_free_result($this->result);
  129. if (isset($this->link)) mysql_close($this->link);
  130. }
  131. }
  132. ?>