first commit
This commit is contained in:
33
Web/imasBackend/getLocalData.php
Normal file
33
Web/imasBackend/getLocalData.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?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);
|
Reference in New Issue
Block a user