简体   繁体   English

在PHP中使用mysql查询返回0个结果

[英]0 results returned with mysql query in PHP

I am trying to create a graph which takes data from one of our databases and counts the number of issues we had for each of the last 12 months. 我正在尝试创建一个图表,该图表从我们的一个数据库中获取数据,并计算过去12个月中每个问题的数量。 The query is running fine but it keeps returning 0 results even though there is records in the database that match the query. 该查询运行正常,但是即使数据库中存在与该查询匹配的记录,该查询仍将返回0结果。 The code i am using is as follows... 我正在使用的代码如下...

 <?php
if (isset($_POST['dept'])) {
    $dept = $_POST['dept'];

} else {

    $dept = '';

}


for ($i = 1; $i <= 12; $i++) {
    $months[] = date("Y-m-d", strtotime(date('Y-m-01') . " -$i months"));

}

foreach ($months as $month => $value) {

    $theMonth = date("M", strtotime($value));
    $theYear  = date("Y", strtotime($value));

    $sql = "SELECT `c_ID`, `department`, `departmentSub`,  `todaysDate`, \n" . "SUM(CASE WHEN department='$dept' AND  departmentSub LIKE 'Loading Error' AND MONTH(todaysDate) = MONTH($value) AND YEAR(todaysDate) = YEAR($value) THEN 1 ELSE 0 END) AS loadingErrors,\n" . "SUM(CASE WHEN department='$dept' AND  departmentSub LIKE 'Bad Customer Service' AND MONTH(todaysDate) = MONTH($value) AND YEAR(todaysDate) = YEAR($value) THEN 1 ELSE 0 END) AS custService\n" . "FROM `nonConformance`";


    if ($result = mysqli_query($conn, $sql)) {
        // Return the number of rows in result set
        while ($row = $result->fetch_assoc()) {
            $loadingErrors = $row['loadingErrors'];
            $custService   = $row['custService'];
        }

    }
}
?>

I cant figure out why this is not returning any results, can anyone help me out with this it would be much appreciated. 我不知道为什么这没有返回任何结果,任何人都可以帮我解决这个问题,对此将不胜感激。

Thanks! 谢谢!

[example data] http://imgur.com/g0UzcpR [示例数据] http://imgur.com/g0UzcpR

In your query, you're not passing through $value as a string. 在查询中,您没有将$value作为字符串传递。 Try surrounding $value with apostrophes: 尝试用撇号将$value括起来:

 $sql = "SELECT `c_ID`, `department`, `departmentSub`,  `todaysDate`, \n" . "SUM(CASE WHEN department='$dept' AND  departmentSub LIKE 'Loading Error' AND MONTH(todaysDate) = MONTH('$value') AND YEAR(todaysDate) = YEAR('$value') THEN 1 ELSE 0 END) AS loadingErrors,\n" . "SUM(CASE WHEN department='$dept' AND  departmentSub LIKE 'Bad Customer Service' AND MONTH(todaysDate) = MONTH('$value') AND YEAR(todaysDate) = YEAR('$value') THEN 1 ELSE 0 END) AS custService\n" . "FROM `nonConformance`";

MONTH($value) becomes MONTH('$value') and YEAR($value) becomes YEAR('$value') MONTH($value)变成MONTH('$value')YEAR($value)变成YEAR('$value')

Also, make sure you're outputting the values of $loadingErrors and $custService within the foreach ($months as $month => $value) { loop, otherwise dates that have zero values will overwrite the previous. 另外,请确保在foreach ($months as $month => $value) {循环内输出$loadingErrors$custServiceforeach ($months as $month => $value) { ,否则具有零值的日期将覆盖前一个值。

Have you tried this in your query? 您是否在查询中尝试过此方法?

Replace 更换

    departmentSub LIKE 'Loading Error'

with

    departmentSub = 'Loading Error'

or 要么

    departmentSub LIKE '%Loading Error%'

If your looking for an exact string value, why use LIKE? 如果您要查找确切的字符串值,为什么要使用LIKE?

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

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