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

42 lines
1.2 KiB
PHP
Executable File

<?php
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(json_encode(["error" => "数据库连接失败"]));
}
// 接收前端传递的 OpenID
$received_openid = isset($_GET['openid']) ? $_GET['openid'] : null;
if ($received_openid) {
// 构建 SQL 查询字符串
$sql = "SELECT * FROM 用户已购课程 WHERE OpenID = '" . $received_openid . "'" . " ORDER BY `购课日期` DESC";
// 执行查询
$result = $conn->query($sql);
// 检查查询结果
if ($result->num_rows > 0) {
// 获取查询结果
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
// 将结果转换为 JSON 格式并输出
echo json_encode(["result" => "拥有课程信息已查询到", "data" => $data]);
} else {
echo json_encode(["result" => "拥有课程信息未查询到", "data" => null]);
}
} else {
echo json_encode(["error" => "未提供 OpenID"]);
}
// 关闭数据库连接
$conn->close();
?>