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