Page 1 of 1

using a query twice?

Posted: Sun Oct 02, 2005 9:40 pm
by s.dot
Is something like this possible?

Code: Select all

$result = mysql_query("SELECT * FROM table");

// use 1
while($array = mysql_fetch_assoc($result))
{
   // do this
}

// use 2
while($array2 = mysql_fetch_assoc($result))
{
   // do this
}
Currently it's not working in my script.. is there a way to make it work?

Posted: Sun Oct 02, 2005 9:55 pm
by feyd
mysql_data_seek()

however, it's recommended (memory allowing) that you cache the original fetches into an array. :)

Posted: Sun Oct 02, 2005 9:59 pm
by s.dot
so if I'd want to return the internal datapointer to the first row (as if I just made the query) I'd use mysql_data_seek($result,0) ?

Posted: Sun Oct 02, 2005 10:02 pm
by Jenk
Following from feyd's comments, I use a function similar to the following for storing results in an array:

Code: Select all

<?php
function getResult ($query, $data = array()) {

    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result)) {

        $data[] = $row;

    }

    return $data;

}
?>
Then you can use $data to index both the rows and the columns, for example $data[0][0] will provide the value of the 1st row, 1st column :)


EDIT: And to your question above, yes.

Posted: Sun Oct 02, 2005 10:03 pm
by s.dot
okay.. that worked. I assume it's correct. guess i'll figure it out when i complete the script

thanks, feyd.. as always :-D