0

所以,我以前遇到过这个问题,从来没有解决过,但现在我又遇到了,我真的希望它得到解决。

在 PHP 文件中,我执行以下几行:

问题出在 querySelect() 中,您可以在下面看到..

$stmt = sqlsrv_query($dbconn, "SELECT * FROM USERS");
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC );
Logger::logg("ROW (not in method): " . var_export($row, true), LV);

querySelect("SELECT * FROM USERS",$dbconn);

我得到以下信息:(第一个输出是正确的并选择了第一个用户,你可以看到返回一个关联数组)。然后在 querySelect $row 中每次都为真。(有更多的输出,因为它为每个用户当然......)

07/25/12 12:11:26 - ROW (not in method): array (
  'LopNr' => 1,
  'Mail' => 'xxxx                                                                                            ',
  'Password' => 'xxx',
  'Auth' => '1',
  'DisplayName' => 'xxx            ',
  'sdsd' => xx,
  'sdsd' => 'xxx',
  'ts' => 1342093599,
  'Cell' => NULL,
  'WantsSMS' => xxx,
).
07/25/12 12:11:27 - Query: SELECT * FROM USERS.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.
07/25/12 12:11:27 - ROW: true.

querySelect 是这样实现的:

function querySelect($query, $dbconn, $fetchLimit = 1000000)
{
    $stmt = sqlsrv_query($dbconn, $query);
    Logger::logg(LOGG_QRY_ERR_VERBOSE . $query, LV);
    if ( !$stmt )
    {
        Logger::logg(LOGG_QRY_ERR);
        throw new Exception(ERR_QUERY);
    }
    $resultArray = array();
    while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) && $fetchLimit > 0)
    {
        Logger::logg("ROW: " . var_export($row, true), LV);
        $resultArray[] = $row;
        $fetchLimit--;
    }
    return $resultArray;
}
4

1 回答 1

1

我猜测

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) && $fetchLimit > 0

被解释为

$row = (sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) && $fetchLimit > 0)

即它包括&& $fetchLimit > 0在作业中。请参阅有关运算符优先级的 PHP 手册

尝试将其更改为

( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) && $fetchLimit > 0

或者

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) and $fetchLimit > 0

(and优先级低于=)

于 2012-07-25T10:47:52.957 回答