Files
YelayMaths/服务端/API/ApiGetWebPagesFromDB.php
2026-09-29 13:39:36 +08:00

44 lines
992 B
PHP
Executable File

<?php
// 声明JSON内容类型
header('Content-Type: application/json');
// 引用配置,建立数据库连接
require_once 'ApiConfig.php';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取前端传入的页码,默认为1
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// 计算查询起始位置
$offset = ($page - 1) * 10;
// 构建SQL查询语句
$sql = "SELECT * FROM 网页列表 ORDER BY 网页id DESC LIMIT $offset, 10";
// 执行查询
$result = $conn->query($sql);
// 处理查询结果
if ($result->num_rows > 0) {
// 将查询结果转换为关联数组
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
// 输出JSON
echo json_encode($rows);
} else {
// 没有结果时输出空数组
echo json_encode(null);
}
// 关闭数据库连接
$conn->close();
?>