2016-07-18 16:39:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class DB {
|
|
|
|
public static function init() {
|
|
|
|
global $uojMySQL;
|
2018-09-20 11:49:26 +00:00
|
|
|
@$uojMySQL = mysqli_connect(UOJConfig::$data['database']['host'] . ':3306', UOJConfig::$data['database']['username'], UOJConfig::$data['database']['password'], UOJConfig::$data['database']['database']);
|
2016-07-18 16:39:37 +00:00
|
|
|
if (!$uojMySQL) {
|
2018-09-20 11:49:26 +00:00
|
|
|
echo 'There is something wrong with database >_<.... ' . mysqli_connect_error();
|
2016-07-18 16:39:37 +00:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function escape($str) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_real_escape_string($uojMySQL, $str);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
2017-11-25 15:29:18 +00:00
|
|
|
public static function fetch($r, $opt = MYSQLI_ASSOC) {
|
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_fetch_array($r, $opt);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function query($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_query($uojMySQL, $q);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function update($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_query($uojMySQL, $q);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function insert($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_query($uojMySQL, $q);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function insert_id() {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_insert_id($uojMySQL);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function delete($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_query($uojMySQL, $q);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function select($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_query($uojMySQL, $q);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
2017-11-25 15:29:18 +00:00
|
|
|
public static function selectAll($q, $opt = MYSQLI_ASSOC) {
|
|
|
|
global $uojMySQL;
|
2016-07-18 16:39:37 +00:00
|
|
|
$res = array();
|
2017-11-25 15:29:18 +00:00
|
|
|
$qr = mysqli_query($uojMySQL, $q);
|
|
|
|
while ($row = mysqli_fetch_array($qr, $opt)) {
|
2016-07-18 16:39:37 +00:00
|
|
|
$res[] = $row;
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
2017-11-25 15:29:18 +00:00
|
|
|
public static function selectFirst($q, $opt = MYSQLI_ASSOC) {
|
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_fetch_array(mysqli_query($uojMySQL, $q), $opt);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function selectCount($q) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
list($cnt) = mysqli_fetch_array(mysqli_query($uojMySQL, $q), MYSQLI_NUM);
|
2016-07-18 16:39:37 +00:00
|
|
|
return $cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkTableExists($name) {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
2016-07-18 16:39:37 +00:00
|
|
|
return DB::query("select 1 from $name") !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function num_rows() {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_num_rows($uojMySQL);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
|
|
|
public static function affected_rows() {
|
2017-11-25 15:29:18 +00:00
|
|
|
global $uojMySQL;
|
|
|
|
return mysqli_affected_rows($uojMySQL);
|
2016-07-18 16:39:37 +00:00
|
|
|
}
|
2017-11-25 15:29:18 +00:00
|
|
|
}
|