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 
halfzebra
  • 6,771
  • 4
  • 32
  • 47
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 15 '15 at 21:30

2 Answers2

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.

Cayce K
  • 2,288
  • 1
  • 22
  • 35
  • And note that in PHP 5.4+, [the `mysqli_result` class](http://php.net/manual/en/class.mysqli-result.php) 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. – Michael Berkowski Jul 15 '15 at 20:40
  • Thanks! I didn't actually know that. So I added it in where it should be best read! – Cayce K Jul 15 '15 at 20:43
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.

henry300
  • 35
  • 8