SQLSRV 在返回查询时跳过一行
SQLSRV skips a row when returning a query
function ambassadorPrimaryMerchantContacts(){
//Load global MSSQL connection
$mssql = mssql_connect();
//Perform query
$result = sqlsrv_query($mssql,
"
EXECUTE Ambassador_MerchantPrimaryContact @AmbassadorID = 1
");
if( $result === false ) {
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
print("SQLSTATE: ".$error[ 'SQLSTATE']."<br />");
print("code: ".$error[ 'code']."<br />");
print( "message: ".$error[ 'message']."<br />");
}
}
}
sqlsrv_next_result($result);
sqlsrv_next_result($result);
sqlsrv_next_result($result);
$merchantContacts = array();
while($row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC))
{
array_push($merchantContacts, array($row[0],$row[1],$row[2],$row[3],$row[4]));
}
return $merchantContacts;
sqlsrv_free_stmt($result);
}
上面的代码做了我想要的,returns 行数与查询相匹配 returns。
但是,我硬编码了三个 sqlsrv_next_result($result);。查询将根据@AmbassadorID 发生变化,因此该部分需要是动态的。我尝试了以下循环:
while(!sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC))
{
sqlsrv_next_result($result);
}
但是这个循环跳过了第一行。我该如何解决这个问题?
谢谢!
使用sqlsrv_has_rows()
。这只是 returns 是否有任何行,但它不获取其中之一。
while (!sqlsrv_has_rows($result)) {
sqlsrv_next_result($result);
}
作为附加选项,您可以尝试使用 do-while
循环并从所有结果集中获取数据。请注意,mssql_connect()
和 sqlsrv_query()
是来自两个不同 PHP 扩展的函数,因此您需要使用 sqlsrv_connect()
.
<?php
function ambassadorPrimaryMerchantContacts(){
// Load global MSSQL connection
$mssql = sqlsrv_connect($server, $cinfo);
// Perform query
$result = sqlsrv_query($mssql, "EXECUTE Ambassador_MerchantPrimaryContact @AmbassadorID = 1");
if ($result === false) {
if (($errors = sqlsrv_errors()) != null) {
foreach ($errors as $error) {
print("SQLSTATE: ".$error['SQLSTATE']."<br />");
print("code: ".$error['code']."<br />");
print("message: ".$error['message']."<br />");
}
}
}
// Fetch data
$merchantContacts = array();
do {
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC))
$merchantContacts[] = $row;
}
} while (sqlsrv_next_result($result));
// End
sqlsrv_free_stmt($result);
return $merchantContacts;
}
?>
function ambassadorPrimaryMerchantContacts(){
//Load global MSSQL connection
$mssql = mssql_connect();
//Perform query
$result = sqlsrv_query($mssql,
"
EXECUTE Ambassador_MerchantPrimaryContact @AmbassadorID = 1
");
if( $result === false ) {
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
print("SQLSTATE: ".$error[ 'SQLSTATE']."<br />");
print("code: ".$error[ 'code']."<br />");
print( "message: ".$error[ 'message']."<br />");
}
}
}
sqlsrv_next_result($result);
sqlsrv_next_result($result);
sqlsrv_next_result($result);
$merchantContacts = array();
while($row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC))
{
array_push($merchantContacts, array($row[0],$row[1],$row[2],$row[3],$row[4]));
}
return $merchantContacts;
sqlsrv_free_stmt($result);
}
上面的代码做了我想要的,returns 行数与查询相匹配 returns。
但是,我硬编码了三个 sqlsrv_next_result($result);。查询将根据@AmbassadorID 发生变化,因此该部分需要是动态的。我尝试了以下循环:
while(!sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC))
{
sqlsrv_next_result($result);
}
但是这个循环跳过了第一行。我该如何解决这个问题?
谢谢!
使用sqlsrv_has_rows()
。这只是 returns 是否有任何行,但它不获取其中之一。
while (!sqlsrv_has_rows($result)) {
sqlsrv_next_result($result);
}
作为附加选项,您可以尝试使用 do-while
循环并从所有结果集中获取数据。请注意,mssql_connect()
和 sqlsrv_query()
是来自两个不同 PHP 扩展的函数,因此您需要使用 sqlsrv_connect()
.
<?php
function ambassadorPrimaryMerchantContacts(){
// Load global MSSQL connection
$mssql = sqlsrv_connect($server, $cinfo);
// Perform query
$result = sqlsrv_query($mssql, "EXECUTE Ambassador_MerchantPrimaryContact @AmbassadorID = 1");
if ($result === false) {
if (($errors = sqlsrv_errors()) != null) {
foreach ($errors as $error) {
print("SQLSTATE: ".$error['SQLSTATE']."<br />");
print("code: ".$error['code']."<br />");
print("message: ".$error['message']."<br />");
}
}
}
// Fetch data
$merchantContacts = array();
do {
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC))
$merchantContacts[] = $row;
}
} while (sqlsrv_next_result($result));
// End
sqlsrv_free_stmt($result);
return $merchantContacts;
}
?>