2

When I do the following:

 while($row = mysql_fetch_array(mysql_query($query))){}

it is infinitive while loop. But this one is not:

 $query = mysql_query($query);
 while($row = mysql_fetch_array($query)){}

What is the difference between them? How does PHP execute?

Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
TranLinh
  • 55
  • 7

4 Answers4

1

Lets assume that you have a table student with following data

id  name
1   JD
2   Dev
3   Pallavi

Now if your $query = "SELECT * FROM student"

CASE: 1

while($row = mysql_fetch_array(mysql_query($query))) { ... }

For the first iteration the execution will be

  1. mysql_query($query)
  2. mysql_fetch_array(get_array_obtined_from_first_step)
  3. Assign the current row to $row

In first step you get all 3 records of the student table. Then you fetch the Record Set in 3rd step you assign the row to $row.

Now as your 3 statements are in the while loop's condition, it will always be TRUE, because it will execute your query each time the loop is iterated, executing all the statements, making it infinite loop.

CASE: 2

$query = mysql_query($query); while($row = mysql_fetch_array($query)){}

Here the query is executed ONCE and then each time the cursor is incremented assigning a single row to $row till the end of Result Set.

Thus when the end of Result Set is achieved it stops.

JDpawar
  • 3,474
  • 2
  • 21
  • 26
0

In the first one, PHP executes the query each time the loop enters new iteration, so the cursor (pointer to the row that goes forward each time you iterate over the result) does not move forward in the array of results. You get the whole bunch of same results each time and each time start from the beginning since the query is just executed.

In the second one your query is executed only once, and then PHP is iterating over the complete array of results while the cursor is moving forward and not being reset to zero on each iteration. That one is probably what you need.

Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
0

Let's look for

while($row = mysql_fetch_array(mysql_query($query))){}

In this, sql query execution is in while loop, means it's recursion itself, hence every time $row will get new data means while loop will not end anywhere.

 $query = mysql_query($query);
 while($row = mysql_fetch_array($query)){}

In this case, you are executing query before while loop. Now you have specific set of result in your $query variable. $query has some limit like 0 or maximum record. Hence loop will stop execution at some point.

AkshayP
  • 2,141
  • 2
  • 18
  • 27
0

How a WHILE loop works:

You need to understand when while loop stops iteration. A while loop looks like this :

while (expression) {
    statement
}

At the beginning it will evaluate the expression first, if it returns TRUE (or which is equivalent to TRUE), then it will execute the statements inside the {...} block, otherwise, it will not execute anything. After it has finished the execution of that part, it will evaluate the expression again. If it returns TRUE again, it will execute scripts again, otherwise, it will stop here. This cycle will continue until the expression returns FALSE.

Difference between two expressions:

Now just look at your expression blocks

mysql_fetch_array(mysql_query($query))

It will always return TRUE, because, it is every time running the mysql_query and mysql_query returning result every time and mysql_fetch_array is always able to fetch the first row, which allows the loop to execute the statement block every time. That's how it becomes an infinite loop.

$row = mysql_fetch_array($query)

Here you have run the query before (only once) and within the while expression you are fetching through the result. You get first row, 2nd row and so on every time you call mysql_fetch_array(). When you finish fetching the last row in the result set, mysql_fetch_array() will return FALSE, which will stop the iteration of while loop.

Recommended Readings:

  1. http://php.net/manual/en/control-structures.while.php
  2. http://php.net/manual/en/function.mysql-fetch-array.php
  3. http://php.net/manual/en/function.mysql-query.php

Note : Consider switchig to mysqli_query() / mysqli_fetch_array() asap. The old functions are not going to be supported by PHP 7.