RE: MS SQL Query question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What's happening is the pointer on the results is advancing until it
hits the end of the result set.  When you run it again, the pointer is
still at the end of the result set and therefore sees nothing 'ahead' of
it.

The same thing can happen with arrays.  I'm not sure if you have to do
it still, but I habituatlly do a "reset($arr);" after I do a sort on the
array because the sort did (still does?) leave the array pointer at the
end of the array, necessitating a 'reset'.

Read some of the notes here:
http://us4.php.net/mysql_fetch_array

This one in particular should help:
********************************
Ben
06-Apr-2004 09:59 
One of the most common mistakes that people make with this function,
when using it multiple times in one script, is that they forget to use
the mysql_data_seek() function to reset the internal data pointer.

When iterating through an array of MySQL results, e.g.

<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   foreach ($line as $col_value) {
       echo $col_value . '<br />';
   }
}
?>

the internal data pointer for the array is advanced, incrementally,
until there are no more elements left in the array. So, basically, if
you copy/pasted the above code into a script TWICE, the second copy
would not create any output. The reason is because the data pointer has
been advanced to the end of the $line array and returned FALSE upon
doing so.

If, for some reason, you wanted to interate through the array a second
time, perhaps grabbing a different piece of data from the same result
set, you would have to make sure you call

<?php
mysql_data_seek($result, 0);
?>

This function resets the pointer and you can re-iterate through the
$line array, again! 
********************************

-TG


> -----Original Message-----
> From: Derrick Hermanson [mailto:dhermanson@xxxxxxxxx] 
> Sent: Tuesday, May 25, 2004 3:05 PM
> To: Ron.Herhuth@xxxxxxxxxxxxxxxxx; php-windows@xxxxxxxxxxxxx
> Subject: RE:  MS SQL Query question
> 
> 
> try something like this
> 
> $result = mssql_query ($query);
> if ($result) {
> 	while ($template_args = msssql_fetch_array ($res)) {
> 		//what ever you want to do with the data
> 	}
> }
> 
> Derrick Hermanson
> Programmer/Analyst I
> Acton International LTD.
> 402.470.5816
> mailto:dhermanson@xxxxxxxxx
> 
> 
> -----Original Message-----
> From: Ron.Herhuth@xxxxxxxxxxxxxxxxx
> [mailto:Ron.Herhuth@xxxxxxxxxxxxxxxxx]
> Sent: Tuesday, May 25, 2004 1:30 PM
> To: php-windows@xxxxxxxxxxxxx
> Subject:  MS SQL Query question
> 
> 
> 
> When I run a query the syntax looks like this:
> 
> $result = mssql_query($query);
> $numRows = mssql_num_rows($result);
> 
> for($i=0; $i<$numRows; $i++)
> {
> $row = mssql_fetch_array($result);
> }
> 
> but if I go to try and loop through the resultset again it wont work
> unless I run this line again "$result = mssql_query($query);"
> 
> I'm sure this is wasting resources but I can't seem to get 
> around it.  Any
> suggestions?
> 
> Thanks,
> Ron
> 
> -- 
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux