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.
 
 
wastesortingserver/database/DbUtil.php

53 lines
1.4 KiB

<?php
require "../config.php";
require "../Log.php";
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("数据库连接失败");
} else {
info("数据库连接成功");
}
return $conn;
} catch (Exception $e) {
error("数据库连接异常");
die("数据库连接异常");
}
}
private static function closeConn(mysqli $conn)
{
try {
if ($conn->close()) {
info("成功关闭数据库");
} else {
error("无法关闭数据库");
}
} catch (Exception $e) {
error("关闭数据库连接出现异常:" . $e);
}
}
// 插入数据
public static function insert($sql, $types, ...$_)
{
try {// 创建连接
$conn = self::getConn();
$stmt = $conn->prepare("$sql");// 参数绑定
$stmt->bind_param($types, ...$_);// 执行sql
$stmt->execute();// 关闭连接
info("执行sql.$sql.成功");
self::closeConn($conn);
} catch (Exception $e) {
error("插入数据异常" . $e);
}
}
}