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 t开发者_运维知识库hanks 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!
I'm assuming mysql_fetch_array() perfroms a loop, so I'm interested in if using a while() in conjunction with it, if it saves a nested loop.
No. mysql_fetch_array
just returns the next row of the result and advances the internal pointer. It doesn't loop. (Internally it may or may not use some loop somewhere, but that's irrelevant.)
while ($row = mysql_fetch_array($result)) {
...
}
This does the following:
mysql_fetch_array
retrieves and returns the next row- the row is assigned to
$row
- the expression is evaluated and if it evaluates to
true
, the contents of the loop are executed - the procedure begins anew
$row = mysql_fetch_array($result); foreach($row as $r) { ... }
This does the following:
mysql_fetch_array
retrieves and returns the next row- the row is assigned to
$row
foreach
loops over the contents of the array and executes the contents of the loop as many times as there are items in the array
In both cases mysql_fetch_array
does exactly the same thing. You have only as many loops as you write. Both constructs do not do the same thing though. The second will only act on one row of the result, while the first will loop over all rows.
It depends how many rows are returned in $results
, and how many columns there are in $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.
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)
).
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
}
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.
$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'];
}
}
精彩评论