我从PHP调用存储过程,它只返回一行(第二行)。但在Management Studio中它返回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;
为什么它只返回一行?
我的存储过程的代码如下:
SELECT 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
答案 0 :(得分:0)
您没有连接正在提取的行。你只需获取,分配给temp,获取另一个,OVERWRITE tmp,再获取另一个,OVERWRITE等等......
试试这个:
$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即可。编辑器将为您设置格式化内容。
答案 1 :(得分:0)
您只获得最后一行,因为在每次迭代中都会覆盖$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);