note 41330 added to function.mysql-fetch-array

From: Date: Tue, 06 Apr 2004 15:59:55 +0000
Subject: note 41330 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-67720@lists.php.net to get a copy of this message
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! ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41330 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41330&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41330&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67720) next »