DB.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class DB {
  3. public static function init() {
  4. global $uojMySQL;
  5. @$uojMySQL = mysqli_connect(UOJConfig::$data['database']['host'] . ':3306', UOJConfig::$data['database']['username'], UOJConfig::$data['database']['password'], UOJConfig::$data['database']['database']);
  6. if (!$uojMySQL) {
  7. echo 'There is something wrong with database >_<.... ' . mysqli_connect_error();
  8. die();
  9. }
  10. }
  11. public static function escape($str) {
  12. global $uojMySQL;
  13. return mysqli_real_escape_string($uojMySQL, $str);
  14. }
  15. public static function fetch($r, $opt = MYSQLI_ASSOC) {
  16. global $uojMySQL;
  17. return mysqli_fetch_array($r, $opt);
  18. }
  19. public static function query($q) {
  20. global $uojMySQL;
  21. return mysqli_query($uojMySQL, $q);
  22. }
  23. public static function update($q) {
  24. global $uojMySQL;
  25. return mysqli_query($uojMySQL, $q);
  26. }
  27. public static function insert($q) {
  28. global $uojMySQL;
  29. return mysqli_query($uojMySQL, $q);
  30. }
  31. public static function insert_id() {
  32. global $uojMySQL;
  33. return mysqli_insert_id($uojMySQL);
  34. }
  35. public static function delete($q) {
  36. global $uojMySQL;
  37. return mysqli_query($uojMySQL, $q);
  38. }
  39. public static function select($q) {
  40. global $uojMySQL;
  41. return mysqli_query($uojMySQL, $q);
  42. }
  43. public static function selectAll($q, $opt = MYSQLI_ASSOC) {
  44. global $uojMySQL;
  45. $res = array();
  46. $qr = mysqli_query($uojMySQL, $q);
  47. while ($row = mysqli_fetch_array($qr, $opt)) {
  48. $res[] = $row;
  49. }
  50. return $res;
  51. }
  52. public static function selectFirst($q, $opt = MYSQLI_ASSOC) {
  53. global $uojMySQL;
  54. return mysqli_fetch_array(mysqli_query($uojMySQL, $q), $opt);
  55. }
  56. public static function selectCount($q) {
  57. global $uojMySQL;
  58. list($cnt) = mysqli_fetch_array(mysqli_query($uojMySQL, $q), MYSQLI_NUM);
  59. return $cnt;
  60. }
  61. public static function checkTableExists($name) {
  62. global $uojMySQL;
  63. return DB::query("select 1 from $name") !== false;
  64. }
  65. public static function num_rows() {
  66. global $uojMySQL;
  67. return mysqli_num_rows($uojMySQL);
  68. }
  69. public static function affected_rows() {
  70. global $uojMySQL;
  71. return mysqli_affected_rows($uojMySQL);
  72. }
  73. }