| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- class db {
- public static $last_query = "";
- public static $queries = [];
- public static $query_count = 0;
- public static $DB = null;
- public static function affected_rows($link = null) {
- if(empty($link)) $link = db::$DB;
- return mysqli_affected_rows($link);
- }
- public static function cast_all_rows($result, $class_name) {
- $objects = [];
- while($row = db::fetch_object($result)) {
- $objects[] = new $class_name($row);
- }
- return $objects;
- }
- public static function connect($host, $username, $password, $database = "", $port = 3306) {
- db::$DB = @new mysqli($host, $username, $password, $database, $port);
- if(!is_null(db::$DB->connect_error)) {
- throw new Exception("Connect Error: " . db::$DB->connect_error);
- }
- db::query("SET NAMES utf8mb4");
- return db::$DB;
- }
- public static function data_seek($result, $row_number) {
- mysqli_data_seek($result, $row_number);
- }
- public static function escape($string) {
- return mysqli_real_escape_string(db::$DB, $string);
- }
- public static function error() {
- return mysqli_error(db::$DB);
- }
- public static function fetch_array($result) {
- return mysqli_fetch_array($result);
- }
- public static function fetch_assoc($result) {
- return mysqli_fetch_assoc($result);
- }
- public static function fetch_object($result) {
- return mysqli_fetch_object($result);
- }
- public static function fetch_row($result) {
- return mysqli_fetch_row($result);
- }
- public static function free_result($result) {
- mysqli_free_result($result);
- }
- public static function get_value_string($pairs, $escape=false) {
- if($escape) {
- $pairs = array_map(array("db", "escape"), $pairs);
- }
- $string_array = array();
- foreach($pairs as $field => $value) {
- if(is_null($value)) {
- $string_array[] = "`$field`=NULL";
- }
- else if(stripos($field, "binary")!==false && stripos($value, "x'")===0) {
- $string_array[] = "`$field`=$value";
- }
- else {
- $string_array[] = "`$field`='$value'";
- }
- }
- return implode(", ", $string_array);
- }
- public static function get_where_string($pairs, $options = []) {
- $escape = false;
- $fuzzy = false;
- if(is_bool($options)) { // Legacy escape
- $escape = $options;
- }
- if(is_array($options)) {
- $escape = $options["escape"] ?? false;
- $fuzzy = $options["fuzzy"] ?? false;
- }
- if($escape) {
- $pairs = array_map(array("db", "escape"), $pairs);
- }
- $string_array = array();
- foreach($pairs as $field => $value) {
- if(is_null($value)) {
- $string_array[] = "`$field` IS NULL";
- }
- else if(is_array($value)) {
- $values = array_map(function($str) {
- return "'$str'";
- }, $value);
- if(empty($value)) $string_array[] = "0"; // Empty array
- else $string_array[] = "`$field` IN (" . implode(", ", $values) . ")";
- }
- else if(is_a($value, "Constraint")) {
- $string_array[] = "`$field` " . $value->getConstraintString();
- }
- else {
- $string_array[] = $fuzzy ? "`$field` LIKE '%$value%'" : "`$field`='$value'";
- }
- }
- return implode(" AND ", $string_array);
- }
- public static function has_index($table, $index_name) {
- $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'";
- $result = db::query($sql);
- return db::num_rows($result)==1;
- }
- public static function insert_id() {
- return mysqli_insert_id(db::$DB);
- }
- public static function num_rows($result) {
- if(is_bool($result)) {
- return 0;
- }
- return mysqli_num_rows($result);
- }
- public static function object_or_false($result) {
- return mysqli_num_rows($result)>0 ? db::fetch_object($result) : false;
- }
- public static function object_with_id($table, $field, $id) {
- $sql = "SELECT * FROM `$table` WHERE `$field`='$id'";
- $result = db::query($sql);
- return db::object_or_false($result);
- }
- public static function ping() {
- return mysqli_ping(db::$DB);
- }
- public static function query($sql) {
- if(!is_string($sql)) return false;
- self::$last_query = $sql;
- $result = mysqli_query(db::$DB, $sql);
- if(!$result) {
- if(!PRODUCTION) {
- throw new Exception("Query failed: $sql");
- }
- else {
- throw new Exception("A database error has occurred. Please try again later.");
- }
- }
- self::$queries[] = $sql;
- self::$query_count++;
- return $result;
- }
- public static function select_db($name, $link_identifier=NULL) {
- if(is_null($link_identifier)) {
- mysqli_select_db(db::$DB, $name);
- }
- else {
- mysqli_select_db($link_identifier, $name);
- }
- }
- public static function thread_id() {
- return mysqli_thread_id(db::$DB);
- }
- }
- ?>
|