MAL - Memória Auxiliar do Lutieri

terça-feira, janeiro 15, 2008

mysql_fetch_array duas vezes

Ben
06-Apr-2004 11: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.

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

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

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

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


Dica retirada daqui:

Marcadores: , ,

0 Comentários:

Postar um comentário

<< Home



Chat with Lutieri G. B.

Subscribe in a reader