0

So, I've had this problem before and never solved it but now I got it again and I really want it to be solved.

In a PHP file I execute the following lines:

The problem is in the querySelect() which you can see down below..

$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);

And I get the following: (The first output is correct and selects the first user and as you can see returns an associcative array). Then in querySelect $row is true each time. (there is more outputs of this because it does it for each user ofcourse...)

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 is implemented like this:

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 Answer 1

1

I suspect

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

is being interpreted as

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

i.e. it's including the && $fetchLimit > 0 in the assignment. See the PHP manual on operator precedence.

Try changing it to

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

or

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

(and has a lower precedence than =)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, now that you're pointing it out, it's so obvious, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.