SQL服务器-我在PHP中只接收第一行结果从查询mssql


sql server - i receive in php only first row of result from query mssql

我从PHP调用一个存储过程,它只返回一行(秒)。但在管理工作室,它返回4行。我使用的是MSSQL Express 2005和sqlsrv驱动程序。在php中,我使用sqlsrv_prepare, sqlsrv_execute和代码进行调用,如:

$swap="";$tmp="";  
while($res=sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_ASSOC))
{$tmp=$res['c_oid']."|".$res['c_time']."|".$res['c_curs']."|".$res['c_name'];}
$swap=$swap."|".$tmp;

为什么它只返回一行?
我的存储过程的代码如下:

选择U1.c_oid

,

        DATEDIFF(s, '1970-01-01 00:00:00', U1.c_timeUpd) as c_time, 
        U1.c_curs,
        OL.c_name,
        DATEDIFF(s, '1970-01-01 00:00:00',GETUTCDATE()) as lastquery
        from t_Updates as U1
        inner join (select t1.c_oid, t1.c_time from t_Updates as t1
                        inner join 
                                (select c_oid, max(c_time)as Date
                                from t_Updates
                                group by c_oid) t2
                    on t2.c_oid = t1.c_oid and t2.Date = t1.c_time and 
                    datediff(s,'1970-01-01 00:00:00',t2.Date)>@date
            )AS U2
        on U1.c_oid = U2.c_oid and U1.c_timeUpd = U2.c_time
        inner join t_ObL as OL
        on U1.c_oid=OL.c_oid
        inner join t_UsersObj
        on t_UsersObj.c_oid = U1.c_oid and  t_UsersObj.c_uid=@uid

您没有连接您正在获取的行。你只需获取,赋值给temp,获取另一个,覆盖tmp,获取另一个,再覆盖,等等…

试试这个:

$swap="";$tmp="";  
while($res=sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_ASSOC)) {
    $tmp=$res['c_oid']."|".$res['c_time']."|".$res['c_curs']."|".$res['c_name'];
    $swap=$swap."|".$tmp; // <--move this inside the loop
}

对于以后的问题,不要使用<code>标签。只需突出显示代码块并单击编辑器中的{}按钮,或按ctrl-K。编辑器会帮你格式化的

您只得到最后一行,因为在每次迭代中您都覆盖了$tmp

$swap = array();
while($res=sqlsrv_fetch_array($this->stmt,SQLSRV_FETCH_ASSOC)) {
  $swap[] =$res['c_oid']."|".$res['c_time']."|".$res['c_curs']."|".$res['c_name'];
}
$swap = '|' . implode('|', $swap);