| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php /** @noinspection PhpUnused */
- class db {
- public static $last_query = "";
- public static $queries = [];
- public static $query_count = 0;
- public static $DB = null;
- public static function affected_rows(mysqli $link = null): int {
- if(empty($link)) $link = db::$DB;
- return mysqli_affected_rows($link);
- }
- /**
- * @param string $host
- * @param string $username
- * @param string $password
- * @param string $database
- * @param int $port
- * @return mysqli
- * @throws Exception
- */
- public static function connect(string $host, string $username, string $password, string $database = "", int $port = 3306): mysqli {
- $link = @new mysqli($host, $username, $password, $database, $port);
- db::$DB = $link;
- if(!is_null($link->connect_error)) {
- throw new Exception("Connect Error: " . $link->connect_error);
- }
- db::query("SET NAMES utf8mb4");
- return $link;
- }
- /**
- * @param mysqli_result $result
- * @param int $row_number
- * @return bool
- */
- public static function data_seek(mysqli_result $result, int $row_number): bool {
- return mysqli_data_seek($result, $row_number);
- }
- /**
- * @param string $string
- * @return string
- */
- public static function escape(string $string): string {
- return mysqli_real_escape_string(db::$DB, $string);
- }
- /**
- * @return string
- */
- public static function error(): string {
- return mysqli_error(db::$DB);
- }
- /**
- * @param mysqli_result $result
- * @return array|null
- */
- public static function fetch_array(mysqli_result $result): ?array {
- return mysqli_fetch_array($result);
- }
- /**
- * @param mysqli_result $result
- * @return array|null
- */
- public static function fetch_assoc(mysqli_result $result): ?array {
- return mysqli_fetch_assoc($result);
- }
- /**
- * @param mysqli_result $result
- * @return object|null
- */
- public static function fetch_object(mysqli_result $result): ?object {
- return mysqli_fetch_object($result);
- }
- /**
- * @param mysqli_result $result
- * @return array|null
- */
- public static function fetch_row(mysqli_result $result): ?array {
- return mysqli_fetch_row($result);
- }
- public static function free_result(mysqli_result $result): void {
- mysqli_free_result($result);
- }
- /**
- * @param array $pairs
- * @param bool $escape
- * @return string
- */
- public static function get_value_string(array $pairs, bool $escape = false): string {
- if($escape) {
- $pairs = array_map(array("db", "escape"), $pairs);
- }
- $string_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);
- }
- /**
- * @param array $pairs
- * @param array|bool $options
- * @return string
- */
- public static function get_where_string(array $pairs, $options = []): string {
- $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 = [];
- 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);
- }
- /**
- * @param string $table
- * @param string $index_name
- * @return bool
- * @throws Exception
- */
- public static function has_index(string $table, string $index_name): bool {
- $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'";
- $result = db::query($sql);
- return db::num_rows($result)==1;
- }
- /**
- * @return int
- */
- public static function insert_id(): int {
- return mysqli_insert_id(db::$DB);
- }
- /**
- * @param mysqli_result $result
- * @return int
- */
- public static function num_rows(mysqli_result $result): int {
- if(is_bool($result)) {
- return 0;
- }
- return mysqli_num_rows($result);
- }
- /**
- * @param mysqli_result $result
- * @return object|null
- */
- public static function object_or_null(mysqli_result $result): ?object {
- return mysqli_num_rows($result)>0 ? db::fetch_object($result) : null;
- }
- /**
- * @param string $table
- * @param string $field
- * @param int $id
- * @return object|null
- * @throws Exception
- */
- public static function object_with_id(string $table, string $field, int $id): ?object {
- $sql = "SELECT * FROM `$table` WHERE `$field`='$id'";
- $result = db::query($sql);
- return db::object_or_null($result);
- }
- /**
- * @return bool
- */
- public static function ping(): bool {
- return mysqli_ping(db::$DB);
- }
- /**
- * @param string $sql
- * @return bool|mysqli_result
- * @throws Exception
- */
- public static function query(string $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;
- }
- /**
- * @param string $name
- * @param mysqli|null $link_identifier
- * @return bool
- */
- public static function select_db(string $name, mysqli $link_identifier = null): bool {
- if(is_null($link_identifier)) {
- return mysqli_select_db(db::$DB, $name);
- }
- else {
- return mysqli_select_db($link_identifier, $name);
- }
- }
- /**
- * @return int
- */
- public static function thread_id(): int {
- return mysqli_thread_id(db::$DB);
- }
- }
|