sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) 返回除空记录外的所有记录
sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) returning all but empty records
我正在创建一个从存储过程中获取数据的 php API,该存储过程在 Dbeaver 上运行良好(检索带值的记录)但我的 api 正在检索没有任何错误的空值。
这是我的代码 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;
我已确保所有列名都正确,但我收到的回复如下:
回复:
[
{
"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
},.......
请帮我解决这个问题
您将 $row
设置为布尔条件 &&
的结果,而不是被提取的行,因为 &&
的优先级高于 =
。
要么将赋值放在括号中,使用 and
而不是优先级较低的 &&
,或者首先测试 $serial_no
。后者在性能方面要好一些,因为当您读取限制时您不会进行额外的提取。
while($serial_no<50 && $row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) //remove this condition later
我正在创建一个从存储过程中获取数据的 php API,该存储过程在 Dbeaver 上运行良好(检索带值的记录)但我的 api 正在检索没有任何错误的空值。
这是我的代码 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;
我已确保所有列名都正确,但我收到的回复如下:
回复:
[
{
"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
},.......
请帮我解决这个问题
您将 $row
设置为布尔条件 &&
的结果,而不是被提取的行,因为 &&
的优先级高于 =
。
要么将赋值放在括号中,使用 and
而不是优先级较低的 &&
,或者首先测试 $serial_no
。后者在性能方面要好一些,因为当您读取限制时您不会进行额外的提取。
while($serial_no<50 && $row=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) //remove this condition later