blessing-skin-server/includes/Database.class.php

89 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* @Author: printempw
* @Date: 2016-02-02 21:59:06
2016-03-06 14:12:12 +08:00
* @Last Modified by: printempw
2016-03-13 11:31:46 +08:00
* @Last Modified time: 2016-03-13 11:27:11
*/
2016-02-06 23:18:07 +08:00
class Database
{
private $connection = null;
function __construct() {
$this->connection = self::checkConfig();
}
public static function checkConfig() {
2016-03-12 18:47:42 +08:00
if (!DEBUG_MODE) error_reporting(0);
2016-03-12 16:46:26 +08:00
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
if ($conn->connect_error) {
Utils::showErrorPage($conn->connect_errno,
"无法连接至 MySQL 服务器。请确认 config.php 中的配置是否正确:".$conn->connect_error);
}
if (!self::checkTableExist($conn)) {
2016-03-13 11:31:46 +08:00
Utils::showErrorPage(-1, "数据库中不存在 ".DB_PREFIX."users 表。请先运行 /admin/install.php 进行安装。");
}
2016-02-03 15:55:52 +08:00
$dir = dirname(dirname(__FILE__));
if (!is_dir("$dir/textures/")) {
Utils::showErrorPage(-1, "textures 文件夹不存在。请先运行 /admin/install.php 进行安装,或者手动放置一个。");
}
return $conn;
}
public static function checkTableExist($conn) {
$sql = "SELECT table_name FROM
2016-03-13 11:31:46 +08:00
`INFORMATION_SCHEMA`.`TABLES` WHERE table_name ='".DB_PREFIX."users'
AND TABLE_SCHEMA='".DB_NAME."'";
return ($conn->query($sql)->num_rows != 0) ? true : false;
}
public function query($sql) {
$result = $this->connection->query($sql);
if (!$this->connection->error) {
return $result;
}
2016-02-06 23:18:07 +08:00
Utils::raise(-1, "Database query error: ".$this->connection->error);
}
public function fetchArray($sql) {
return $this->query($sql)->fetch_array();
}
public function select($key, $value) {
2016-03-13 11:31:46 +08:00
return $this->fetchArray("SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'");
}
public function getNumRows($key, $value) {
2016-03-13 11:31:46 +08:00
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE $key='$value'";
return $this->query($sql)->num_rows;
}
2016-03-06 14:12:12 +08:00
public function getRecordNum() {
2016-03-13 11:31:46 +08:00
$sql = "SELECT * FROM ".DB_PREFIX."users WHERE 1";
2016-03-06 14:12:12 +08:00
return $this->query($sql)->num_rows;
}
public function checkRecordExist($key, $value) {
return ($this->getNumRows($key, $value) != 0) ? true : false;
}
public function insert($array) {
$uname = $array['uname'];
$passwd = $array['passwd'];
$ip = $array['ip'];
2016-03-13 11:31:46 +08:00
$sql = "INSERT INTO ".DB_PREFIX."users (username, password, ip, preference)
VALUES ('$uname', '$passwd', '$ip', 'default')";
return $this->query($sql);
}
public function update($uname, $key, $value) {
2016-03-13 11:31:46 +08:00
return $this->query("UPDATE ".DB_PREFIX."users SET `$key`='$value' WHERE username='$uname'");
}
public function delete($uname) {
2016-03-13 11:31:46 +08:00
return $this->query("DELETE FROM ".DB_PREFIX."users WHERE username='$uname'");
}
}