正在运行的php代码获得null响应时。 这是我的代码
<?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);
?>
如果我获取语句“ echo json_encode($ fetchgenders_sql);”并在数据库中运行,显示数据。 但是在运行php时在$ row中得到空响应。 返回无效null([])
的sex_arr答案 0 :(得分:1)
一个意外结果的可能解释是,您的存储过程可能返回多个结果集。这样做的一个原因是,当您错过将SET NOCOUNT ON作为存储过程的第一行时。然后受影响的行数就是结果集的一部分。
您可以尝试使用sqlsrv_next_result()来检索所有结果集:
<?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);
?>