来自COUNT SQL的0结果通过PHP

时间:2014-03-05 18:01:58

标签: php excel tsql

我可以运行这些查询:

"SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = 'myusername' and date >= '2014 03 01 00:00:00' and page LIKE '%Order%'"

在Microsoft SQL管理工作室中,我得到了我期望的结果,数字在60年代,但是当我运行此代码并通过PHP生成一个excel文档时,它返回所有0的

<?php
include 'sqlconn.php';

$filename = "userAccessReport.xls";
$reportFile = fopen("somdirectory/".$filename, "w"); 


fwrite($reportFile,"<table><tr><td>username</td><td>visits this month ".date("Y m")." 01 00:00:00</td><td>page</td></tr>");

$users = odbc_exec($live, "SELECT uname FROM usepeeps");
while (odbc_fetch_array($users))
{

    $username=odbc_result($users,'uname');        

    fwrite($reportFile,"<tr><td>".$username."</td><td></td><td></td></tr>");

    $orderWell = odbc_exec($live, "SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = '".$username."' and date >= '".date("Y m")." 01 00:00:00' and page LIKE '%Order%'");
    while(odbc_fetch_array($orderWell)){fwrite($reportFile,"<tr><td></td><td>".odbc_result($orderWell,'userNum')."-</td><td>Order Well</td></tr>");}

    $receipts = odbc_exec($live, "SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = '".$username."' and date >= '".date("Y m")." 01 00:00:00' and page LIKE '%Receipt%'");
    while(odbc_fetch_array($receipts)){fwrite($reportFile,"<tr><td></td><td>".odbc_result($receipts,'userNum')."-</td><td>Receipts</td></tr>");}

    $inventory = odbc_exec($live, "SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = '".$username."' and date >= '".date("Y m")." 01 00:00:00' and page LIKE '%Inventory%'");
    while(odbc_fetch_array($inventory)){fwrite($reportFile,"<tr><td></td><td>".odbc_result($inventory,'userNum')."-</td><td>Inventory</td></tr>");}

    $einv = odbc_exec($live, "SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = '".$username."' and date >= '".date("Y m")." 01 00:00:00' and pageLIKE '%Invoicing%'");
    while(odbc_fetch_array($einv)){fwrite($reportFile,"<tr><td></td><td>".odbc_result($einv,'userNum')."-</td><td>E-Invoicing</td></tr>");}

    $brdroom = odbc_exec($live, "SELECT COUNT(uname) as userNum FROM loginhistory WHERE user = '".$username."' and date >= '".date("Y m")." 01 00:00:00' and page LIKE '%Boardroom%'");
    while(odbc_fetch_array($brdroom)){fwrite($reportFile,"<tr><td></td><td>".odbc_result($brdroom,'userNum')."-</td><td>Boardroom Booking</td></tr>");}

}

fwrite($reportFile,"</table>");
fclose($reportFile);
echo "/dev/tempfiles/" . $filename;

1 个答案:

答案 0 :(得分:0)

您永远无法捕获fetch来电的结果,实际上会丢失所有结果。你应该有更多的东西:

while($row = odbc_fetch_array($einv)) {
     echo $row['userNum'];
}

由于您的fetch_array()调用会提取并停用 ONE 行的结果数据,因此odbc_result()无需处理任何内容。