开发者

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

开发者 https://www.devze.com 2023-02-28 01:49 出处:网络
I am calling a stored procedure from PHP and it returns just one row (second). But in Management Studio it returns 4 rows. I am using MSSQL Express 2005 and the sqlsrv driver.

I am calling a stored procedure from PHP and it returns just one row (second). But in Management Studio it returns 4 rows. I am using MSSQL Express 2005 and the sqlsrv driver. In php I am doing the call using sqlsrv_prepare, sqlsrv_execute and and code like:

$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;

Why does it only return one row?

Code of my stored procedure is like:

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开发者_StackOverflow中文版(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


You're not concatenating the rows you're fetching. You simply fetch, assign to temp, fetch another, OVERWRITE tmp, fetch another, OVERWRITE again, etc...

Try this:

$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
}

For future questions, don't use <code> tags. Just highlight the code block and click the {} button in the editor, or hit ctrl-K. the editor will take care of formatting things for you.


You get only the last row, because in every iteration you overwrite $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);
0

精彩评论

暂无评论...
验证码 换一张
取 消