blessing-skin-server/libraries/Option.class.php

83 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/**
* @Author: printempw
* @Date: 2016-03-18 14:02:12
* @Last Modified by: printempw
2016-03-27 11:38:27 +08:00
* @Last Modified time: 2016-03-27 11:28:24
*/
2016-03-18 17:49:52 +08:00
use Database\Database;
2016-03-26 22:29:45 +08:00
class Option
{
public static function get($key) {
$conn = Database::checkConfig();
$sql = "SELECT * FROM ".DB_PREFIX."options WHERE `option_name` = '$key'";
$result = $conn->query($sql);
if ($conn->error)
2016-03-27 11:38:27 +08:00
throw new E("Database query error: ".$conn->error, -1);
return $result->fetch_array()['option_value'];
}
public static function set($key, $value) {
2016-03-26 22:50:19 +08:00
$conn = Database::checkConfig();
if (!self::has($key)) {
self::add($key, $value);
} else {
$sql = "UPDATE ".DB_PREFIX."options SET `option_value`='$value' WHERE `option_name`='$key'";
$result = $conn->query($sql);
if ($conn->error)
2016-03-27 11:38:27 +08:00
throw new E("Database query error: ".$conn->error, -1);
2016-03-26 22:50:19 +08:00
else
return true;
}
}
public static function add($key, $value) {
$conn = Database::checkConfig();
// check if option exists
if (!self::has($key)) {
$sql = "INSERT INTO ".DB_PREFIX."options (`option_name`, `option_value`) VALUES ('$key', '$value')";
$result = $conn->query($sql);
if ($conn->error)
2016-03-27 11:38:27 +08:00
throw new E("Database query error: ".$conn->error, -1);
2016-03-26 22:50:19 +08:00
else
return true;
} else {
return true;
}
}
public static function has($key) {
$conn = Database::checkConfig();
// check if option exists
$sql = "SELECT * FROM ".DB_PREFIX."options WHERE `option_name` = '$key'";
if ($conn->query($sql)->num_rows != 0) {
return true;
} else {
return false;
}
}
public static function delete($key) {
$conn = Database::checkConfig();
if (self::has($key)) {
$sql = "DELETE FROM ".DB_PREFIX."options WHERE `option_name`='$key'";
$result = $conn->query($sql);
if ($conn->error)
2016-03-27 11:38:27 +08:00
throw new E("Database query error: ".$conn->error, -1);
2016-03-26 22:50:19 +08:00
else
return true;
} else {
return false;
}
}
public static function setArray($options) {
foreach ($options as $key => $value) {
self::set($key, $value);
}
return true;
}
}