0

I'm trying to do the following:

while ($row = mysql_fetch_assoc($result){
    ...
}
...
while ($row2 = mysql_fetch_assoc($result){
    ...
} 

but it doesn't work. The commands within the 2nd loop never run. I'm guessing that this has to do with the pointer of the mysql function, which has to be reset. If so, how to do this?

Chris
  • 57,622
  • 19
  • 111
  • 137
  • possible duplicate of [How to go through mysql result twice?](http://stackoverflow.com/questions/6439230/how-to-go-through-mysql-result-twice) – MSadura Jun 18 '14 at 10:11
  • @MarkS, yup. I was searching for reset pointer and thats why I coulnd't find. – Chris Jun 18 '14 at 10:21

3 Answers3

4

use the function mysql_data_seek

http://www.php.net/manual/en/function.mysql-data-seek.php

Though you should look at upgrading to not use mysql_* functions

exussum
  • 18,275
  • 8
  • 32
  • 65
3

Avoid any functions starting with mysql_, they are deprecated. Using PDO, you can do what you want like so:

$pdo = new PDO(/**/);                // [1]

$stmt = $pdo->prepare(/* query */);  // [2]
$stmt->execute();                    // [3]

$rows = $stmt->fetchAll();           // [4]

foreach($rows as $row)
{
    //
}

foreach($rows as $row2)
{
    //
}

Guides for lines referenced above:

Marty
  • 39,033
  • 19
  • 93
  • 162
1
$result2 = $result;
while ($row = mysql_fetch_assoc($result){
    ...
}

...

while ($row2 = mysql_fetch_assoc($result2){
    ...
} 
Samar Haider
  • 862
  • 10
  • 21