sqlsrv_fetch_array在php中返回true

时间:2012-07-25 10:19:11

标签: php sql-server

所以,我以前遇到过这个问题并且从未解决过这个问题,但现在我又得到了它,我真的希望它能够得到解决。

在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每次都为true。 (这有更多的输出,因为它为每个用户做了...)

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;
}

1 个答案:

答案 0 :(得分:1)

我怀疑

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

被解释为

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

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

尝试将其更改为

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

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

and的优先级低于=