为什么我的代码没有在图表中检索数据?

时间:2016-01-27 07:46:12

标签: php sql-server graph while-loop

<?php

    session_start();
    include "../../lib/mssql.connect.php";

    $params = array($_POST['year'], sha1($_POST['month']));
    $sql ="SELECT count(*) AS TotalGuestBook_TotalCount, MONTH(GuestBook_CreatedDate) as mth from tbl_Guestbook
           WHERE YEAR(GuestBook_CreatedDate) ='2015'
           group by MONTH(GuestBook_CreatedDate)"; 

    // run the query
    $result = sqlsrv_query($conn, $sql, $params)

        while($row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC))
        {
            $dashboardResult[] = array('label' =>$row['GuestBook_CreatedDate'], 'values' =>$row['TotalGuestBook_TotalCount']);
        }
    echo json_encode($dashboardResult);
?>

2 个答案:

答案 0 :(得分:0)

不要在查询执行中包含$params 使用:

$result = sqlsrv_query($conn, $sql);

而不是:

$result = sqlsrv_query($conn, $sql, $params)

答案 1 :(得分:0)

是的,这是一个解析错误,你错过了这行中的终止半冒号:

$result = sqlsrv_query($conn, $sql, $params)

这应该是:

$result = sqlsrv_query($conn, $sql, $params);

对于未定义的索引错误,您使用了错误的列名:

$dashboardResult[] = array('label' =>$row['GuestBook_CreatedDate'], 'values' =>$row['TotalGuestBook_TotalCount']);

应该是:

$dashboardResult[] = array('label' =>$row['mth'], 'values' =>$row['TotalGuestBook_TotalCount']);

您正在将索引名称定义为$row['GuestBook_CreatedDate'],但在查询中,您将此字段的别名用作mth