stefmikhail
stefmikhail

Reputation: 7125

Using Same Variable in Multiple PHP `while` Loops

I have a PHP page with multiple mysql_query instances. Almost every one uses a while loop to retrieve multiple rows of data.

As I keep a snippet of this often used code, and the example I was taught with, used the variable $row, I have multiple instances of the following on my page:

while ($row = mysql_fetch_assoc($foo_data)) { 
    $barArray[] = $row['barValue']; 
}

I even have an instance of $row = mysql_fetch_assoc($foo_data) without a while loop.

I'm wondering if the multiple uses of $row as a variable on a single page, is all right?

The PHP is functioning fine, but I always want to be sure that my code is proper and conforms to standard rules.

Thank you.

Upvotes: 0

Views: 291

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157839

yes, it's all right.
it's all right to use the same spoon when you're eating soup.

Upvotes: 3

Alex
Alex

Reputation: 9471

Yup, not a problem at all.

Just like always using $i as your count in a for loop, you can repeatedly use $row to no ill effect.

Upvotes: 2

Naftali
Naftali

Reputation: 146302

Yes that is fine. but if you try:

var_dump($row);

at the end of your code -- it will only return that last value of $row

Upvotes: 2

Related Questions