简体   繁体   English

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

[英]MySQL query GROUP works in console, does not work in PHP script

I created a function with this mysqli query but for some reason it does not print the desired result in php script. 我用这个mysqli查询创建了一个函数,但由于某种原因它不能在php脚本中打印出所需的结果。 What am I doing wrong? 我究竟做错了什么?

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

update: this is my function. 更新:这是我的功能。 It should find persons working in a day having those qualifications and count them 它应该找到在具有这些资格的一天工作并计算它们的人

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;
}

I store the result in an array so I can count them. 我将结果存储在一个数组中,以便我可以计算它们。

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

I am using it like this: 我这样使用它:

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

They are all variables. 它们都是变数。 It should show the counted number of people on shift having these specific qualifications. 它应该显示具有这些特定资格的轮班人数。

You placed wrong order of params in bind_param() function. 你在bind_param()函数中放置了错误的params顺序。 You should add params value in order of "?" 你应该按照“?”的顺序添加params值。 you used in your query. 您在查询中使用过。 Please do following changes: 请进行以下更改:

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

Hope it may helpful to you!! 希望它对你有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM