tags:

views:

38

answers:

2

Why am I not able to get inside the while loop in the getCustomers() function?

$stores = $bl->getStoresForGuide($gID);  //Returns 6 stores
$storelist = getStoreList($stores);      //Generate the HTML for the store list
$brandlist = getCustomers($stores);      //Generate the HTML for brand list

function getStoreList($stores) 
{
  while ($row = mysql_fetch_array($stores)) {
    // Do stuff 
  }
 //return result
}


function getCustomers($stores)
{
  echo mysql_num_rows($stores);  //Outputs 6

  while ($row = mysql_fetch_array($stores)) {
    echo "test "; // Outputs nothing
  }
  // Return some result
}
+5  A: 

You're looping twice. The first time you loop, you get to the end, and the pointer isn't reset before you loop again.

If you're sure you want to do this, check out mysql_data_seek, and seek to the beginning of the result. Otherwise, I'd recommend just storing the results and iterating over the array.

jvenema
Thanks. Didn't know that. I put the result in an array, and it works fine now.
Steven
A: 

You are calling getStoreList first, then by the time you call getCustomers, $stores has already had all its rows fetched.

echo