AJAX调用PHP生成介绍
<?php
// 假设这是数据库连接
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 查询数据
$sql = "SELECT id, name, age FROM your_table";
$result = $conn->query($sql);
// 初始化数组
$data = array();
// 如果有数据则将其添加到数组
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
// 设置内容类型为JSON
header('Content-Type: application/json');
// 输出数据
echo json_encode($data);
$conn->close();
?>
评论留言