简体   繁体   English

相同的查询,php vs phpmyadmin中的结果不同-为什么?

[英]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.

相关问题 MySQL 使用相同的查询在 PHP 和 PHPMyadmin 上返回不同的结果 - MySQL returning Different results on PHP and PHPMyadmin using same Query PHP:PDO查询不返回任何结果,但是同一查询在phpmyadmin中返回4个结果? - PHP: PDO Query returns no results, but the same query returns 4 results in phpmyadmin? PHP与MySQL Workbench:相同的查询,不同的结果 - PHP vs MySQL Workbench: Same query, different results 相同的mysql查询在phpmyadmin中的不同时间给出不同的结果 - Same mysql query gives different results at different times in phpmyadmin mysql和php%like%查询。 可在phpmyadmin中工作,但php的结果不同 - mysql and php %like% query. Works in phpmyadmin but different results php 相同的联接查询:通过Phalcon和phpMyAdmin获得两个不同的结果 - Same join query: two different results through Phalcon and phpMyAdmin 即使查询相同,PHP返回的结果也比phpmyadmin少 - PHP returning less results than phpmyadmin even though the query is the same PHP中带有&#39;&&#39;的MySQL查询返回的结果与phpMyAdmin的结果不同 - MySQL query in PHP with '&' returns different results than phpMyAdmin does Wordpress sql 查询结果与 sql 查询在 phpmyadmin 查询不同的顺序 - Wordpress sql query results vs sql query in phpmyadmin query different order PHP和MySQL通过相同的查询显示不同的结果 - PHP and MySQL showing different results with same query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM