sqlsrv_fetch_array missing the first row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

sqlsrv_fetch_array missing the first row

Post by chopWood »

The following will print out 9 rows however there are acutually 10 rows in the fetch array. The first one is always missing. Any clue why it skips the first $row? Thank you.

// Fetch and print all the records....
$bg = '#99cccc';
while($row = sqlsrv_fetch_array($result)) {
$bg = ($bg=='#99cccc' ? '#ffffff' : '#99cccc');
echo '<tr bgcolor="' . $bg . '">
<td align="left">'. $row['LastName'] . '</td>
<td align="left">' . $row['ItemId'] . '</td>
<td align="left">' . $row['Description'] . '</td>
<td align="left">' . number_format($row['ListPrice'],2) . '</td>
<td align="left">' . $row['ItemStatus'] . '</td>
<td align="left">' . $row['DateTimePosted'] . '<br /></td>
</tr>
';
} // End of WHILE loop.
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: sqlsrv_fetch_array missing the first row

Post by mrcoffee »

My guess is that something is moving the result pointer before you call sqlsrv_fetch_array...

When you execute a query (e.g., with mysql_query), the result pointer starts on index 0 (row 1), but various functions will move the pointer. For example:

Code: Select all

$q = mysql_query("SELECT * FROM table LIMIT 10"); // assume result is 10 rows, result pointer is on index 0
$r1 = mysql_result($q,2,'id'); // id of 2nd index (3rd result), pointer is now on index 3
$r2 = mysql_fetch_assoc($q); // calling without a while statement... moves the pointer by one, now we're on index 4
$r3 = mysql_fetch_assoc($q); // and now on index 5
while($r = mysql_fetch_assoc($q)) {
     echo $r['id'].'<br>'; // result indexes 5,6,7,8,9
}
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: sqlsrv_fetch_array missing the first row

Post by chopWood »

thank you for the clue... that's what happened. Everything fixed.
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: sqlsrv_fetch_array missing the first row

Post by chopWood »

I have the statement:
$sold_array = sqlsrv_fetch_array ($result);
Before the while loop which must put the pointer ahead 1
Is there a way to reset the pointer to the beginning?
Post Reply