You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.4 KiB
133 lines
4.4 KiB
<?php
|
|
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
|
|
require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php";
|
|
|
|
interface DoExcute
|
|
{
|
|
// 参数处理
|
|
function bind_param(mysqli_stmt $stmt, array $param = null);
|
|
|
|
// 执行结果处理
|
|
function doResult(mysqli_stmt $stmt);
|
|
}
|
|
|
|
|
|
interface DoConn
|
|
{
|
|
// 连接关闭前处理
|
|
function before_close(mysqli $conn);
|
|
}
|
|
|
|
class DbUtil
|
|
{
|
|
|
|
private static function getConn()
|
|
{
|
|
try {
|
|
$conn = new mysqli(db_config["servername"], db_config["username"], db_config["password"], db_config["dbname"]);
|
|
if ($conn->connect_error) {
|
|
error("数据库连接失败,请检查数据库配置" . "服务器:" . db_config["servername"] . ",用户名:" . db_config["username"] . ",密码:" . db_config["password"] . ",数据库:" . db_config["dbname"] . "是否正确");
|
|
throw new Exception("数据库连接失败");
|
|
} else {
|
|
info("数据库连接成功");
|
|
}
|
|
return $conn;
|
|
} catch (Exception $e) {
|
|
error("数据库连接异常" . $e);
|
|
}
|
|
}
|
|
|
|
private static function closeConn(mysqli $conn)
|
|
{
|
|
try {
|
|
if ($conn->close()) {
|
|
info("成功关闭数据库");
|
|
} else {
|
|
error("无法关闭数据库");
|
|
}
|
|
} catch (Exception $e) {
|
|
error("关闭数据库连接出现异常:" . $e);
|
|
}
|
|
}
|
|
|
|
// 插入数据
|
|
public static function modify($action, $sql, DoConn $do_conn = null, $types = null, ...$_)
|
|
{
|
|
|
|
try {// 创建连接
|
|
$conn = self::getConn();
|
|
if (!is_null($conn)) {
|
|
$stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位
|
|
if ($stmt instanceof mysqli_stmt) {
|
|
if (!is_null($types)) {
|
|
|
|
if (strlen($types) == count($_)) {
|
|
$stmt->bind_param($types, ...$_);// 参数绑定
|
|
} else {
|
|
throw new Exception("预处理sql" . $sql . ",参数绑定类型" . $types . "跟参数数量" . var_dump($_) . "不匹配");
|
|
}
|
|
}
|
|
$stmt->execute();// 执行sql
|
|
info($action . $stmt->affected_rows . "条数据");
|
|
$stmt->close();
|
|
info("执行sql.$sql.成功");
|
|
if (!is_null($do_conn)) {
|
|
$do_conn->before_close($conn);
|
|
}
|
|
} else {
|
|
throw new Exception("预处理sql异常,无法处理sql" . $sql . ".");
|
|
}
|
|
self::closeConn($conn);// 关闭连接
|
|
}
|
|
} catch (Exception $e) {
|
|
error($action . "数据异常" . $e);
|
|
}
|
|
}
|
|
|
|
// 插入数据
|
|
public static function insert($sql, $db_conn = null, $types = null, ...$_)
|
|
{
|
|
self::modify("插入", $sql, $db_conn, $types, ...$_);
|
|
}
|
|
|
|
// 更新数据
|
|
public static function update($sql, $db_conn = null, $types = null, ...$_)
|
|
{
|
|
self::modify("更新", $sql, $db_conn, $types, ...$_);
|
|
}
|
|
|
|
// 删除数据
|
|
public static function delete($sql, $db_conn = null, $types = null, ...$_)
|
|
{
|
|
self::modify("删除", $sql, $db_conn, $types, ...$_);
|
|
}
|
|
|
|
|
|
//查询数据
|
|
public static function query($sql, DoExcute $doExcute, array $param = null)
|
|
{
|
|
try {// 创建连接
|
|
$conn = self::getConn();
|
|
if (!is_null($conn)) {
|
|
$stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位
|
|
if ($stmt instanceof mysqli_stmt) {
|
|
$doExcute->bind_param($stmt, $param);
|
|
$stmt->execute();// 执行sql
|
|
$doExcute->doResult($stmt);
|
|
info("执行sql.$sql.成功");
|
|
$stmt->close();
|
|
} else {
|
|
throw new Exception("预处理sql异常,无法处理sql" . $sql . ".");
|
|
}
|
|
} else {
|
|
throw new Exception("无法连接数据库");
|
|
}
|
|
// 关闭连接
|
|
self::closeConn($conn);
|
|
} catch (Exception $e) {
|
|
error("查询数据异常" . $e);
|
|
throw $e;
|
|
}
|
|
}
|
|
} |