Home > Blockchain >  sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) returning all but empty records
sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) returning all but empty records

Time:03-28

I am creating a php API that fetches data from a stored procedure, the stored procedure is working fine on Dbeaver (retrieving records with values) but my api is retrieving null values without any error.

Here is the code from my api

$sql="exec ERP_Tracking.dbo.GET_SECURITYBRIEF_DATA";
try{
    $stmt = sqlsrv_query($connection_crm,$sql);
}
catch(exception $e)
{
    $indi="from query exec";
    $error=$e;
}


$serial_no=0;
try
{
    while($row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) && $serial_no<50) //remove this condition later
    {
         
         $srow=[
            "S.NO"=>$serial_no,
            "1"=>$row["DOC"],  //DOC
            "2"=>$row["[AGRN #]"], //AGRN #
            "3"=>$row["[VEH REG #]"], //VEH_REG #
            "4"=>$row["ENGINE"], //ENGINE
            "5"=>$row["CHASIS"], //CHASIS
            "6"=>$row["[CUSTOMER TYPE]"],  //CUSTOMER_TYPE
            "7"=>$row["[CUSTOMER NAME]"], //CUSTOMER_NAME
            "8"=>$row["[FATHER NAME]"], //FATHER_NAME 
            
     
        ];
        array_push($row_array,$srow);
        //array_push($row_array,"counter checking");
        $serial_no =1;
    }
}
catch(Exception $e)
{
    $indi="from while";
    $error=$e;
}



ob_start();
header('Content-type: application/json');
if($error!='no error')
{
    //echo json_encode($error);
    echo json_encode($indi);
}
else
{
    echo json_encode($row_array);
}
header("Connection: close");
header("Content-length: " . (string)ob_get_length());
ob_end_flush();
die;

I have made sure that all column names are correct but I am getting response like this:

Response:

[
    {
        "S.NO": 0,
        "1": null,
        "2": null,
        "3": null,
        "4": null,
        "5": null,
        "6": null,
        "7": null,
        "8": null,
        "9": null,
        "10": null,
        "11": null,
        "12": null,
        "13": null,
        "14": null,
        "15": null,
        "16": null,
        "17": null,
        "18": null,
        "19": null,
        "20": null,
        "21": null,
        "22": null,
        "23": null,
        "24": null,
        "25": null,
        "26": null,
        "27": null,
        "28": null,
        "29": null,
        "30": null,
        "31": null,
        "32": null,
        "33": null,
        "34": null,
        "35": null,
        "36": null,
        "37": null,
        "38": null,
        "39": null,
        "40": null,
        "41": null,
        "42": null
    },
    {
        "S.NO": 1,
        "1": null,
        "2": null,
        "3": null,
        "4": null,
        "5": null,
        "6": null,
        "7": null,
        "8": null,
        "9": null,
        "10": null,
        "11": null,
        "12": null,
        "13": null,
        "14": null,
        "15": null,
        "16": null,
        "17": null,
        "18": null,
        "19": null,
        "20": null,
        "21": null,
        "22": null,
        "23": null,
        "24": null,
        "25": null,
        "26": null,
        "27": null,
        "28": null,
        "29": null,
        "30": null,
        "31": null,
        "32": null,
        "33": null,
        "34": null,
        "35": null,
        "36": null,
        "37": null,
        "38": null,
        "39": null,
        "40": null,
        "41": null,
        "42": null
    },.......

Please help me with this

CodePudding user response:

You're setting $row to the result of the boolean && condition, not the row being fetched, because && has higher precedence than =.

Either put the assignment in parentheses, use and instead of &&, which has low precedence, or test $serial_no first. The latter is a little better for performance, since you don't do an extra fetch when you read the limit.

    while($serial_no<50 && $row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) //remove this condition later
  • Related