doubushi0031 2015-01-28 09:33
浏览 36

PHP SQL Server查询运行不在PHP中工作

I currently have a query that is working in MS SQL Server Management studio, without any problems, when i run the query in php it just doesn't return anything.

Here is the query and PHP code

     Public function GetClockedHours() {
    $conn = odbc_connect('easydo', '', '');
    if (!$conn) {
        exit("Connection Failed: " . $conn);
    }
    var_dump($conn);
    $sql = " WITH ByDays AS ( 
            SELECT 
                CHINA_VISION_DorEvents.EventTm AS T,
                CONVERT(VARCHAR(10),CHINA_VISION_DorEvents.EventTm,102) AS Day,
                FLOOR(CONVERT(FLOAT,CHINA_VISION_DorEvents.EventTm)) DayNumber,
                ROW_NUMBER() OVER(PARTITION BY FLOOR(CONVERT(FLOAT,CHINA_VISION_DorEvents.EventTm)) ORDER BY CHINA_VISION_DorEvents.EventTm) InDay 
            FROM CHINA_VISION_DorEvents
            Where CHINA_VISION_DorEvents.DorCtrls_Ref = '16' AND CHINA_VISION_DorEvents.CardCode = '000006f1' AND CONVERT(Date,CHINA_VISION_DorEvents.EventTm) > dateadd(day, -7, getdate())
            )

            ,Diffs AS (
            SELECT 
                E.Day,
                E.T ET, O.T OT, O.T-E.T Diff, 
                DATEDIFF(S,E.T,O.T) DiffSeconds -- difference in seconds
            FROM 
                (SELECT BE.T, BE.Day, BE.InDay 
                 FROM ByDays BE 
                 WHERE BE.InDay % 2 = 1) E -- Even rows
            INNER JOIN
                (SELECT BO.T, BO.Day, BO.InDay 
                 FROM ByDays BO 
                 WHERE BO.InDay % 2 = 0) O -- Odd rows
            ON E.InDay + 1 = O.InDay -- Join rows (1,2), (3,4) and so on
               AND E.Day = O.Day --  in the same day
            )


            SELECT Day, 
                SUM(DiffSeconds) Seconds, 
                CONVERT(VARCHAR(8), 
                (DATEADD(S, SUM(DiffSeconds), '1900-01-01T00:00:00')),
                108) TotalHHMMSS -- The same, formatted as HH:MM:SS
            FROM Diffs GROUP BY Day
            ORDER BY Day desc  ";

    $rs = odbc_exec($conn, $sql);
    if (!$rs) {
        exit("Error in SQL");
    }else{
        echo "Get Connection";
    }
    var_dump($rs);
            var_dump($sql);
    $array = array();
    $i = 1;
    var_dump(odbc_fetch_array($rs, $i));
    while ($row = odbc_fetch_array($rs, $i)) {
        foreach ($row AS $key => $value) {
            $array[$i][$key] = $row[$key];
        }
        $i++;
    }
     var_dump($row);
    var_dump($array);
    return $array;
}

The PHP Part of this code work, even though it's not the best way to connect to the database, however looking at what is returned at each stage there is just nothing being returned.

When i cut the query down to just the first select.

SELECT 
CHINA_VISION_DorEvents.EventTm AS T,
CONVERT(VARCHAR(10),CHINA_VISION_DorEvents.EventTm,102) AS Day,
FLOOR(CONVERT(FLOAT,CHINA_VISION_DorEvents.EventTm)) DayNumber,
ROW_NUMBER() OVER(PARTITION BY FLOOR(CONVERT(FLOAT,CHINA_VISION_DorEvents.EventTm)) ORDER BY CHINA_VISION_DorEvents.EventTm) InDay 
FROM CHINA_VISION_DorEvents
Where CHINA_VISION_DorEvents.DorCtrls_Ref = '16' AND CHINA_VISION_DorEvents.CardCode = '000006f1' AND CONVERT(Date,CHINA_VISION_DorEvents.EventTm) > dateadd(day, -7, getdate())

Without the WITH ByDays part the query actually returned what i would expect it to return at that point of the query, so with the "WITH ByDays AS (" section and everything that follows on the query won't work in php why is this and how can i fix this

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改