[英]Same query, different results in php vs phpmyadmin- Why?
I am trying to get the total actions from each employee in a MySQL database. 我正在尝试从MySQL数据库中的每个员工获得总行动。 My script is giving me a significantly lower total than the number in the database for all employees.
我的脚本给我的总数远低于数据库中所有雇员的总数。 I am using a SUM function in the query.
我在查询中使用SUM函数。 The query works fine in phpmyadmin, but not in my script.
该查询在phpmyadmin中工作正常,但在我的脚本中却无法正常工作。 Any ideas why this is happening.
任何想法为什么会这样。
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>';
}
$result->free();
}
When I run this script, employee 1005 has 63 actions. 当我运行此脚本时,员工1005有63个动作。 However, when I run this query in phpmyadmin, employee 1005 has 194 actions (which is correct).
但是,当我在phpmyadmin中运行此查询时,员工1005有194个操作(正确)。 All employees have fewer actions in the output of the script.
所有员工在脚本输出中的动作都更少。 The interesting thing is that the $count variable outputs the correct amount, which is the total of all actions... Please help with this glitch.
有趣的是$ count变量输出正确的数量,该数量是所有操作的总和。
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>'; //tag mismatch
}
$result->free();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.