代码拉取完成,页面将自动刷新
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "liu";
// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 查询每个部门的员工数量和性别统计
$sql = "SELECT d.department,COUNT(e.id) AS total_employees,
SUM(CASE WHEN e.gender = '0' THEN 1 ELSE 0 END) AS male_count,
SUM(CASE WHEN e.gender = '1' THEN 1 ELSE 0 END) AS female_count
FROM
employees e
LEFT JOIN
department d ON e.depart_id = d.id
GROUP BY
d.department";
$result = $conn->query($sql);
// 准备以JSON格式输出的数据
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = [
'department' => $row['department'],
'totalEmployees' => (int)$row['total_employees'],
'maleCount' => (int)$row['male_count'],
'femaleCount' => (int)$row['female_count']
];
}
// 查询某员工的工资数据
$employeeId = 1; // 示例员工ID
$sqlSalary = "SELECT date, salary FROM salaryr WHERE employee_id = $employeeId ORDER BY date";
$resultSalary = $conn->query($sqlSalary);
// 准备以JSON格式输出的工资数据
$salaryData = [];
while ($row = $resultSalary->fetch_assoc()) {
$salaryData[] = [
'date' => $row['date'],
'salary' => (float)$row['salary']
];
}
// 关闭数据库连接
$conn->close();
// 将数据编码为JSON格式,用于AJAX响应
header('Content-Type: application/json');
echo json_encode(['departmentData' => $data, 'salaryData' => $salaryData]);
?>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。