Files
IMAS/Web/imasBackend/getLocalData.php
2025-01-10 15:20:33 +08:00

34 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
include_once "db_config.php"; // 数据库配置文件
date_default_timezone_set('Asia/Shanghai');
header("Content-Type: application/json"); // 返回 JSON 格式的响应
header("Access-Control-Allow-Origin: *"); // 允许跨域请求
header("Access-Control-Allow-Methods: POST, OPTIONS"); // 允许的方法
header("Access-Control-Allow-Headers: Content-Type"); // 允许的请求头
// 这里假设连接已经建立,并且您有 $link 变量代表数据库连接
// 定义SQL查询获取最近的10条记录
$query = "SELECT * FROM localdata ORDER BY time DESC LIMIT 1";
// 执行查询
$result = mysqli_query($conn, $query);
// 检查查询结果
if (!$result) {
die("查询失败: " . mysqli_error($conn)); // 处理错误
}
// 处理查询结果
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row; // 将每一行数据存入数组
}
// 返回 JSON 格式的数据
header("Content-Type: application/json");
echo json_encode($data);
// 关闭数据库连接
mysqli_close($conn);