4

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
}

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.