[php] while ($row = mysql_fetch_array($result)) - how many loops are being performed?

if...

$query = "SELECT col1,col2,col3 FROM table WHERE id > 100"
$result = mysql_query($query);

for this action:

while ($row = mysql_fetch_array($result)){
   ....
}

is this doing 1 loop (iterated x times)?

and for this one:

$row = mysql_fetch_array($result)
foreach($row as $r){
   ...
}

is this doing 2 loops (iterated x times)?

where x is the number of results


EDIT:

ok thanks guys, ok I basically phrased this question really, really badly.

in retrospect it should have been

'does mysql_fetch_array() only return one row each time it is called'

I an now happy that my understanding of mysql_fetch_array() was v. incorrect!

thanks for your time!

This question is related to php mysql

The answer is


Yes, mysql_fetch_array() only returns one result. If you want to retrieve more than one row, you need to put the function call in a while loop.

Two examples:

This will only return the first row

$row = mysql_fetch_array($result);

This will return one row on each loop, until no more rows are available from the result set

while($row = mysql_fetch_array($result))
{
    //Do stuff with contents of $row
}

$query = "SELECT col1,col2,col3 FROM table WHERE id > 100"
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
 while($row = mysql_fetch_array()) //here you can use many functions such as mysql_fetch_assoc() and other
 {
//It returns 1 row to your variable that becomes array and automatically go to the next result string
  Echo $row['col1']."|".Echo $row['col2']."|".Echo $row['col2'];
 }
}

For the first one: your program will go through the loop once for every row in the result set returned by the query. You can know in advance how many results there are by using mysql_num_rows().

For the second one: this time you are only using one row of the result set and you are doing something for each of the columns. That's what the foreach language construct does: it goes through the body of the loop for each entry in the array $row. The number of times the program will go through the loop is knowable in advance: it will go through once for every column in the result set (which presumably you know, but if you need to determine it you can use count($row)).


Considering only one row is returned, only one loop will be done in both cases. Though it will check for the loop entering condition twice on each.


The first row:

$result = mysql_query($query);  

return php db resource.

The second row

while ($row = mysql_fetch_array($result))  

Loops all the records returned by the query.

Use mysql_fetch_assoc instead
In that case the row has key=>value pairs.
In the while just put this:
print_r($row) and you will understand
If you use mysql_fetch_assoc the format of the row will be:

$row["column1_name"] = column1_value;  
$row["column2_name"] = column2_value;  

For this one:

$row = mysql_fetch_assoc($result)  
foreach ($row as $columnName => $columnValue) {  
    ...  
}  

You will get the firs row from the query and you will iterate all columns in the first result of the query.


It depends how many rows are returned in $results, and how many columns there are in $row?