Asaf Sitner
Asaf Sitner

Reputation: 629

Stored Procedure returning result twice

I'm trying to use a stored procedure to query some data from my database, but I'm getting the results twice for each field.

The stored procedure looks like this (creation code):

PROCEDURE [dbo].[msp_Query_Information]
@category_id tinyint
AS
BEGIN
        SET NOCOUNT ON;
        SELECT  ,[Name]
                ,[Status_ID]
                ,CONVERT(char(19), [Update_Time], 120)
                ,[Max_Value]
                ,[Current_Value]
        FROM [dbo].[MyTable]
        WHERE [Category_ID] = @category_id
END

I'm using the following PHP to call the stored procedure (minus error checks and debug code):

<?php
    //define db connection
    $serverName = '(local)';
    $connectionInfo = array("Database"=>"MyDatabase");

    //connect to database
    $dbConn = sqlsrv_connect($serverName, $connectionInfo);

    //execute procedure to query servers status
    $spSql = "{call [dbo].[msp_Query_Information](?)}";
    $id = 2;
    $params = array($id);

    $result = sqlsrv_query($dbConn, $spSql, $params);

    //show results
    while ($row = sqlsrv_fetch_array($result))
    {
        foreach ($row as $key => $value) 
        {
            echo $value.'<br/>';
        }
    }

    //close connection to database
    $closeConn = sqlsrv_close($dbConn);

    echo '<br/>Database connection closed.';
?>

These are the results I'm getting:

TEST
TEST
2
2
2012-09-30 16:03:00
2012-09-30 16:03:00
200
200
0
0

There is only one record in MyTable, with Category_ID set to 2. All the data is correct, I'm simply getting it twice.

Am I doing something wrong here?

Is it a normal behaviour and I'm simply too much of a newbie to know?

Upvotes: 1

Views: 762

Answers (2)

dani herrera
dani herrera

Reputation: 51665

Revise fetchType:

while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC ))

Quoting doc:

A predefined constant specifying the type of array to return. Possible values are SQLSRV_FETCH_ASSOC, SQLSRV_FETCH_NUMERIC, and SQLSRV_FETCH_BOTH (the default).

Upvotes: 1

mpora
mpora

Reputation: 1479

Try to rewrite your code as described here: http://php.net/manual/en/function.sqlsrv-fetch-array.php

Upvotes: 0

Related Questions