簡體   English   中英

我在php中僅收到查詢mssql結果的第一行

[英]i receive in php only first row of result from query mssql

我從PHP調用存儲過程,它僅返回一行(第二行)。 但是在Management Studio中,它返回4行。 我正在使用MSSQL Express 2005和sqlsrv驅動程序。 在php中,我正在使用sqlsrv_preparesqlsrv_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

您不是在串聯要獲取的行。 您只需獲取,分配給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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM