0

While running php code getting null response . here is my code

<?php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET,POST, OPTIONS");
header('Content-Type: application/json');


// include database and object files
include_once '../config/database.php';

$data = json_decode(file_get_contents("php://input"));


$CIFNO=$data->CIFNO;
$PUBLIC_KEY=$data->PUBLIC_KEY ;



//Read Query

      $fetchgenders_sql=

      " exec USP_GET_CUSTOMER_INFO_BY_CIF"
       ." @CIFNO ='009',  @PUBLIC_KEY ='1556422589arijit11111199' ";

//echo json_encode($fetchgenders_sql);
$getgenderResults= sqlsrv_query($conn, $fetchgenders_sql);


if ($getgenderResults == FALSE)
 die(print_r(sqlsrv_errors()));

 $gender_arr = array(); 
while ($row = sqlsrv_fetch_array($getgenderResults, SQLSRV_FETCH_ASSOC)) {

$gender_arr[]=   $row; 

}
if (empty($gender_arr)) {
        $genders = array(
                "status" => "400",
                "fieldName"=> "gender",
                "defaultMessage" => "Sorry not found");  
         }else{
$genders = array(
        "status" => "200",
        "genders" => $gender_arr);
}
//print_r(json_encode($organizationType));
sqlsrv_close($conn); //Close the connnectiokn first

echo json_encode($genders);
 ?>

if I fetch the statement "echo json_encode($fetchgenders_sql);" and run in database ,data is showing. but while running php getting null response in $row. gender_arr returning null([])

1 Answer 1

1

One possible explanation for your unexpected result is the fact, that your stored procedure may return multiple result sets. One reason for this is when you miss to put SET NOCOUNT ON as first line in your stored procedure. Then the count of affected rows is part of the result sets.

You may try to use sqlsrv_next_result() to retrieve all your result sets:

<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET,POST, OPTIONS");
header('Content-Type: application/json');

// Include database and object files
include_once '../config/database.php';

$data = json_decode(file_get_contents("php://input"));
$CIFNO = $data->CIFNO;
$PUBLIC_KEY = $data->PUBLIC_KEY ;

// Query
$fetchgenders_sql = "exec USP_GET_CUSTOMER_INFO_BY_CIF @CIFNO = '009', @PUBLIC_KEY ='1556422589arijit11111199'";
$getgenderResults = sqlsrv_query($conn, $fetchgenders_sql);
if ($getgenderResults === false) {
    die(print_r(sqlsrv_errors()));
}
$gender_arr = array();
do { 
    while ($row = sqlsrv_fetch_array($getgenderResults, SQLSRV_FETCH_ASSOC)) {
        $gender_arr[] = $row; 
    }
} while sqlsrv_next_result($getgenderResults);  

// Result
if (empty($gender_arr)) {
    $genders = array(
                "status" => "400",
                "fieldName"=> "gender",
                "defaultMessage" => "Sorry not found"
    );  
} else {
    $genders = array(
        "status" => "200",
        "genders" => $gender_arr
    );
}

// End
sqlsrv_close($conn); //Close the connnectiokn first
echo json_encode($genders);
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.