I've got the following script:


$sql = "SELECT * FROM table";
$query = mysql_query($sql);

while($array = mysql_fetch_array($query))
{
echo $array[row]."<br>";
}

while($array = mysql_fetch_array($query))
{
echo $array[row]."<br>";
}

I basically want to take and loop through the values of a table. But I want to do this twice, maybe even more than twice, but it won't work unless I redeclare the $query after every time I run fetch_array. I only want to have to make one query, because I may have to run the while() loop a whole lot of times, and that could possibly be a lot of queries. I was thinking maybe the pointer for the array has been set to the end and isn't set back to the start when I try fetch_array() again. I've tried running reset() on $array but that won't work either. Anybody have any idea as to how to fix this problem?

    This basically copies the mysql_results array to a new array you can reuse.

    $sql = "SELECT * FROM table";
    $query = mysql_query($sql);
    while($array = mysql_fetch_array($query)) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$results[] = $array[row];
    }
    for ($x = 0; $x < sizeof($results); $x++) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $results[$x]."<br>";
    }

    Hope this helps.

      Mm-k, I'll try that out. I might just do something that calculates how many while loops I'll need and just execute that code that many times so I can have enough need arrays. Thanks.

        Well that sort of...halfway helps. It allows me to set to an array one row. I want to in a sense...copy a whole array and be able to use it over and over again without having to run a query to get it. Your idea works if I only needed one value of the array (like $array[name]), but now if I want to have an exact copy of the array.

          Write a Reply...