1
0

db.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. class db {
  3. public static $last_query = "";
  4. public static $queries = [];
  5. public static $query_count = 0;
  6. public static $DB = null;
  7. public static function affected_rows($link = null) {
  8. if(empty($link)) $link = db::$DB;
  9. return mysqli_affected_rows($link);
  10. }
  11. public static function cast_all_rows($result, $class_name) {
  12. $objects = [];
  13. while($row = db::fetch_object($result)) {
  14. $objects[] = new $class_name($row);
  15. }
  16. return $objects;
  17. }
  18. public static function connect($host, $username, $password, $database = "", $port = 3306) {
  19. db::$DB = @new mysqli($host, $username, $password, $database, $port);
  20. if(!is_null(db::$DB->connect_error)) {
  21. throw new Exception("Connect Error: " . db::$DB->connect_error);
  22. }
  23. db::query("SET NAMES utf8mb4");
  24. return db::$DB;
  25. }
  26. public static function data_seek($result, $row_number) {
  27. mysqli_data_seek($result, $row_number);
  28. }
  29. public static function escape($string) {
  30. return mysqli_real_escape_string(db::$DB, $string);
  31. }
  32. public static function error() {
  33. return mysqli_error(db::$DB);
  34. }
  35. public static function fetch_array($result) {
  36. return mysqli_fetch_array($result);
  37. }
  38. public static function fetch_assoc($result) {
  39. return mysqli_fetch_assoc($result);
  40. }
  41. public static function fetch_object($result) {
  42. return mysqli_fetch_object($result);
  43. }
  44. public static function fetch_row($result) {
  45. return mysqli_fetch_row($result);
  46. }
  47. public static function free_result($result) {
  48. mysqli_free_result($result);
  49. }
  50. public static function get_value_string($pairs, $escape=false) {
  51. if($escape) {
  52. $pairs = array_map(array("db", "escape"), $pairs);
  53. }
  54. $string_array = array();
  55. foreach($pairs as $field => $value) {
  56. if(is_null($value)) {
  57. $string_array[] = "`$field`=NULL";
  58. }
  59. else if(stripos($field, "binary")!==false && stripos($value, "x'")===0) {
  60. $string_array[] = "`$field`=$value";
  61. }
  62. else {
  63. $string_array[] = "`$field`='$value'";
  64. }
  65. }
  66. return implode(", ", $string_array);
  67. }
  68. public static function get_where_string($pairs, $options = []) {
  69. $escape = false;
  70. $fuzzy = false;
  71. if(is_bool($options)) { // Legacy escape
  72. $escape = $options;
  73. }
  74. if(is_array($options)) {
  75. $escape = $options["escape"] ?? false;
  76. $fuzzy = $options["fuzzy"] ?? false;
  77. }
  78. if($escape) {
  79. $pairs = array_map(array("db", "escape"), $pairs);
  80. }
  81. $string_array = array();
  82. foreach($pairs as $field => $value) {
  83. if(is_null($value)) {
  84. $string_array[] = "`$field` IS NULL";
  85. }
  86. else if(is_array($value)) {
  87. $values = array_map(function($str) {
  88. return "'$str'";
  89. }, $value);
  90. if(empty($value)) $string_array[] = "0"; // Empty array
  91. else $string_array[] = "`$field` IN (" . implode(", ", $values) . ")";
  92. }
  93. else if(is_a($value, "Constraint")) {
  94. $string_array[] = "`$field` " . $value->getConstraintString();
  95. }
  96. else {
  97. $string_array[] = $fuzzy ? "`$field` LIKE '%$value%'" : "`$field`='$value'";
  98. }
  99. }
  100. return implode(" AND ", $string_array);
  101. }
  102. public static function has_index($table, $index_name) {
  103. $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'";
  104. $result = db::query($sql);
  105. return db::num_rows($result)==1;
  106. }
  107. public static function insert_id() {
  108. return mysqli_insert_id(db::$DB);
  109. }
  110. public static function num_rows($result) {
  111. if(is_bool($result)) {
  112. return 0;
  113. }
  114. return mysqli_num_rows($result);
  115. }
  116. public static function object_or_false($result) {
  117. return mysqli_num_rows($result)>0 ? db::fetch_object($result) : false;
  118. }
  119. public static function object_with_id($table, $field, $id) {
  120. $sql = "SELECT * FROM `$table` WHERE `$field`='$id'";
  121. $result = db::query($sql);
  122. return db::object_or_false($result);
  123. }
  124. public static function ping() {
  125. return mysqli_ping(db::$DB);
  126. }
  127. public static function query($sql) {
  128. if(!is_string($sql)) return false;
  129. self::$last_query = $sql;
  130. $result = mysqli_query(db::$DB, $sql);
  131. if(!$result) {
  132. if(!PRODUCTION) {
  133. throw new Exception("Query failed: $sql");
  134. }
  135. else {
  136. throw new Exception("A database error has occurred. Please try again later.");
  137. }
  138. }
  139. self::$queries[] = $sql;
  140. self::$query_count++;
  141. return $result;
  142. }
  143. public static function select_db($name, $link_identifier=NULL) {
  144. if(is_null($link_identifier)) {
  145. mysqli_select_db(db::$DB, $name);
  146. }
  147. else {
  148. mysqli_select_db($link_identifier, $name);
  149. }
  150. }
  151. public static function thread_id() {
  152. return mysqli_thread_id(db::$DB);
  153. }
  154. }
  155. ?>