代碼以下:sql
/* * 按月新建數據表 * */ public function create_tables(){ //獲取年月 $year_time = date('Ym'); $db = Db::connect('log'); Db::startTrans(); try { $sql = "CREATE TABLE `$year_time` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增ID', `api` varchar(100) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL COMMENT '請求API', `qs` varchar(100) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT '' COMMENT '參數', `time` int(10) UNSIGNED NOT NULL COMMENT '請求時間', `resp` json NOT NULL COMMENT '返回的JSON結果', PRIMARY KEY (`id`) USING BTREE, INDEX `idx_request`(`api`, `qs`, `time`) USING BTREE ) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '接口請求記錄表' ROW_FORMAT = Dynamic;"; $db->query($sql); // 提交事務 Db::commit(); } catch (\Exception $e) { // 回滾事務 Db::rollback(); } }