Hi.
Im having a script looking something like this:

$sql = "SELECT * FROM Table WHERE columnID='3'";
$var = @mysql_query($sql);
$res = @mysql_fetch_array($var);
$num = @mysql_num_rows($var);
echo "The result from ".$res['name']." returned ".$num." rows<br>";

$counter = 1;
while($resarray = @mysql_fetch_array($var)){
  echo "Result ".$counter.": ".$resarray['value']."";
  $counter++;
} //end while

First of all I must say that all functions have been tested before I've added the @ to the mysql_xxxx commands.

The problem is that when I run the while-loop it seems to be missing the first result(row) from the query. By this I mean that if $num returned 5 rows, the while loop will only return the last 4 of them.

Anybody know why, and the best workaround for this ?
--> I've tried to run the query again, and that of course works, but is this necessary to make it work(it will "double" the server load doing the query over again) ?
--> This is the "workaround doing a new query:

$counter = 1;
$newvar = mysql_query($var); //doing the query one more time
while($resarray = @mysql_fetch_array($newvar)){ //fetch array from new query result
  echo "Result ".$counter.": ".$resarray['value']."";
  $counter++;
} //end while

Help would be much appreciated.

    By calling mysql_fetch_array before the loop, you are setting the internal result pointer of the query to the next result. So that when you enter the loop, you are already at the second result record.

    Call mysql_data_seek($result, 0); to reset the internal pointer back to the first record.

      That did it :-)

      Seems I still have plenty of mysql_xxxx to learn :-)

        Write a Reply...