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 modify($action, $sql, DoConn $do_conn = null, $types = "", ...$_) { try {// 创建连接 $conn = self::getConn(); $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 if (!empty($types)) { $stmt->bind_param($types, ...$_);// 参数绑定 } $stmt->execute();// 执行sql info($action . $stmt->affected_rows . "条数据"); $stmt->close(); info("执行sql.$sql.成功"); if (!empty($do_conn)) { $do_conn->before_close($conn); } self::closeConn($conn);// 关闭连接 } catch (Exception $e) { error($action . "数据异常" . $e); } } // 插入数据 public static function insert($sql, $types = "", ...$_) { self::modify("插入", $sql, null, $types, ...$_); } public static function insert_with_param($sql, $types, $db_conn, ...$_) { self::modify("插入", $sql, $db_conn, $types, ...$_); } // 更新数据 public static function update($sql, $types = "", ...$_) { self::modify("更新", $sql, null, $types, ...$_); } // 删除数据 public static function delete($sql, $types = "", ...$_) { self::modify("删除", $sql, null, $types, ...$_); } //查询数据 public static function query($sql, DoExcute $doExcute, array $param = null) { try {// 创建连接 $conn = self::getConn(); $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 $doExcute->bind_param($stmt, $param); $stmt->execute();// 执行sql $doExcute->doResult($stmt); info("执行sql.$sql.成功"); $stmt->close(); // 关闭连接 self::closeConn($conn); } catch (Exception $e) { error("查询数据异常" . $e); } } }