MySQL查询GROUP在控制台中工作,在PHP脚本中不工作

时间:2019-06-03 06:01:10

标签: php mysqli

我使用此mysqli查询创建了一个函数,但由于某种原因,它无法在php脚本中打印所需的结果。 我在做什么错了?

SELECT  COUNT(*)
FROM (
SELECT id
FROM personal WHERE structure = ?
WHERE id IN 
(SELECT id FROM presence WHERE service = 'shift1' AND year = ? AND month = ? AND day = ? ) 
AND id IN 
(SELECT id FROM qualification WHERE idqualification = '1' or idqualification = '2')
ORDER BY RAND()
) t
GROUP BY t.id

更新:这是我的功能。它应该找到一天中具有这些资格的工作人员并对其进行计数

function presence_by_qualification($structure, $day, $month, $year)
{
global $connection;
$stmt = $connection->prepare("SELECT  COUNT(*)
FROM (
SELECT id
FROM personal WHERE structure = ?
WHERE id IN 
(SELECT id FROM presence WHERE service = 'shift1' AND year = ? AND month = ? AND day = ? ) 
AND id IN 
(SELECT id FROM qualification WHERE idqualification = '1' or idqualification = '2')
ORDER BY RAND()
) t
GROUP BY t.id");
$stmt->bind_param('ssss', $structure, $day, $month, $year);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($counts);
results = $stmt -> fetch();
return $results;
}

我将结果存储在一个数组中,以便可以对其进行计数。

$presencebyqualif[] = presence_by_qualification($structure, $day, $month, $year);

我正在这样使用它:

$structure = "1029";
$day = "01";
$month = "05";
$year = "2019";

它们都是变量。它应该显示具有这些特定资格的轮班人数。

1 个答案:

答案 0 :(得分:0)

您在bind_param()函数中放置了错误的参数顺序。您应按“?”的顺序添加参数值。您在查询中使用的。请进行以下更改:

$stmt->bind_param('ssss', $structure, $year, $month, $day);

希望对您有帮助!