




本文详解如何将 sql 查询结果按日期归类,并以清晰、可读的结构(如“2025-02-15 -----”后接带短横线的条目)写入 txt 文件,避免原始扁平化输出,提升导出数据的实用性与可读性。
在实际 Web 应用中,用户常需按时间维度(如某年某月)导出结构化日志、笔记或业务记录。但直接遍历查询结果并逐字段拼接(如原代码中 fwrite($fh, $item)),极易导致语义混乱、日期重复、格式僵硬等问题。要实现目标格式:
2025-02-15 ----- - lorem ipsum - dolor sit amet 2025-02-20 ----- - consectetur adipiscing elit
关键在于按日期分组 + 控制段落结构 + 安全字符串处理。以下是优化后的完整实现方案:
$user = $sanitize->for_db($_POST['user']); $date = $_POST['date']; // 假设格式为 '2025-02'(年-月) // ✅ 更安全的日期过滤:利用 LIKE '2025-02-%' 避免 SQL 注入风险(已预处理 $user) $sql = "SELECT * FROM `table_name` WHERE `user` = ? AND `date` LIKE ? ORDER BY `date` DESC"; $stmt = $database->prepare($sql); $stmt->bind_param('ss', $user, $date . '-%'); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 0) { $error[] = "Sorry, there was an error and the data was not exported."; } else { $file = 'path/to/' . $user . '.txt'; $fh = fopen($file, 'a'); if (!$fh) { $error[] = "Failed to open file for writing."; } else { $current_date = ''; // 缓存当前处理的日期(用于分组判断) $output = ''; while ($r = $result->fetch_assoc()) { $formatted_date = date('F d, Y', strtotime($r['date'])); // 如:February 15, 2025 // ✅ 检测日期变更:触发新日期标题行 if ($r['date'] !== $current_date) { if (!empty($current_date)) { $output .= "\n"; // 上一组结束后空一行 } $output .= $formatted_date . " -----\n"; $current_date = $r['date']; } // ✅ 添加条目(假设内容字段名为 'data';请根据实际表结构调整) $output .= "- " . trim((string)$r['data']) . "\n"; } fwrite($fh, $output); fclose($fh); $success[] = "Your data has been exported successfully!"; } }
$ts = strtotime($r['date']);
$formatted_date = $ts ? date('F d, Y', $ts) : 'Invalid Date';实现美观、结构化的文本导出,核心不在于“怎么写文件”,而在于“如何组织数据逻辑”。通过缓存上一个日期值,在循环中动态判断是否需要插入分组标题,即可自然生成层次分明的输出。配合预处理语句与健壮的错误处理,该方案既安全可靠,又易于维护和扩展——例如后续可轻松支持 CSV/JSON 导出、添加时间范围摘要,或集成 ZIP 批量打包功能。