0

Why don't people use for ,foreach or do..while() and why omit increment counter in while?

`<?php  while ( $fa = mysql_fetch_array($sel1) )//uesd mysql_fetch_array() function in while loop
    { 
          echo  $fa['cid'];//display client id 
    }

// while Syntax in w3school give to used this

 $x = 1; 

 while($x <= 5) {
     echo "The number is: $x";
     $x++;
 }  
?>//show why we don't used mysql_fecth_array() like this 
1

2 Answers 2

2

This is kind of dated information. In one way you're asking a question that is just accepted along the community. It is understood better that while I have information to show ... do something. You generally wouldn't say foreach of these items ... do something though you could. However, the other problem is mysql_fetch_array returns FALSE if there are no more rows. This would not work in a foreach because it is not an array. A for would also fail because to check for the finishing of a for you have to go to some point and end.. FALSE is not a valid point of check (that I have ever tried or used).


@Michael Berkowski Adds:

As of PHP 5.4+, the mysqli_result class does have Iterator support, meaning you can do $result = mysqli_query(...); and subsequently foreach ($result as $row) {...} and it will fetch associative arrays

Though this isn't to be said the most commonly used form which is why we have the question.


You could do...while but why would you. You don't have most the information that you're going to need from the fetch array.

While is accepted and generally better. Doesn't fail and has a fall back if the mysql fails any way.

Lastly... don't use mysql_* anymore. Switch to mysqli_*. Safer.. smarter.. better.

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

2 Comments

And note that in PHP 5.4+, the mysqli_result class does have Iterator support, meaning you can do $result = mysqli_query(...); and subsequently foreach ($result as $row) {...} and it will fetch associative arrays. So, the while loop can be replaced with a foreach in more recent PHP/mysqli. Same with PDO.
Thanks! I didn't actually know that. So I added it in where it should be best read!
0

You can perfectly use any other loop to get the query results.
The reason why people mostly use while loop is because it simply is the easiest way. The reason for that is that without any extra line of code we don't know how many rows were returned and so we don't know how many times we have to iterate to get all the rows from the result.
Everytime mysql_fetch_array() is executed, it gives us the next row of data and when it runs out of rows it returns false. So using while loop we basically say: While there is still a new row, we take the data from it, fetch the next row and when we run out of rows, we stop.

You can accomplish the same with for loop for example. It's just not that straightforward.

Using for loop to iterate mysql_fetch_array() :

$r = mysql_query($query);
$num_rows = mysql_num_rows($r);

for($i = 0; $i < $num_rows; $i++) {
    echo mysql_fetch_array()[$id];
}

Other choice would be to place an if statement inside the for loop to check if there are any new rows, when not, we can exit the for loop.
Using the while loop we kinda combine the loop and the if statement.

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.