0

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.

3 Answers 3

3

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

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

1 Comment

But remember to lick all the soup off first.
2

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

3 Comments

So what you're saying is, if I use $row, and it has been used before, then if overwrites its previous value(s) with a new one. And that this is ok because the value that is being overwritten, has already been used in whatever operation was contained within the previous while loop, etc. Does that sound about right? Maybe not eloquent...
the previous while loop is over. PHP runs in order.
Exactly. Wonderful. And much appreciated!
2

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.

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.