前端:test.html
后端:test.php
$type = $_POST['type'];
switch ($type) {
case 'banji':
$sql = 'SELECT DISTINCT `banji` FROM `表名` ORDER BY `banji` ASC';
break;
case 'xuesheng':
$sql = 'SELECT `id`,`xingming` FROM `表名` WHERE `banji`=' . $_POST['banji'] . ' ORDER BY `id` ASC';
break;
case 'chengji':
$sql = 'SELECT `shuxue`,`yuwen` FROM `表名` WHERE `id`=' . $_POST['xuesheng'] . ' LIMIT 1';
break;
default:
return;
}
$pdo = new PDO('mysql:host=数据库服务器IP;dbname=数据库名', '用户名', '密码');
$data = $pdo->query($sql);
$list = [];
foreach ($data as $v) {
switch ($type) {
case 'banji':
$list[] = $v['banji'];
break;
case 'xuesheng':
$list[$v['id']] = $v['xingming'];
break;
case 'chengji':
$list['shuxue'] = $v['shuxue'];
$list['yuwen'] = $v['yuwen'];
break;
default:
return;
}
}
echo json_encode($list);
数据库: