Forum Moderators: coopster

Message Too Old, No Replies

PHP/MySQL Query Question

         

StupidScript

11:10 pm on Sep 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What are the advantages or disadvantages to using one of these two methods:

$qry=mysql_query("select * from stuff");

1)
$row_qry=mysql_fetch_assoc($qry);
do {
...
} while ($row_qry=mysql_fetch_assoc($qry));

2)
while($row=mysql_fetch_array($qry)) {
...
}

Thanks in advance.

IanKelley

2:53 am on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Method 2 will use slightly less memory.

Also, in the case of large datasets where the while loop has a conditional break method 2 would be more efficient.

coopster

1:03 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



My question would be which are you comparing? The logical structure of the loop itself or the two different mysql_fetch functions?

StupidScript

5:50 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The two fetch functions, and then the loop implementation, secondarily. Would using switch->case be more efficient than either of the first two loops?

coopster

6:21 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with
MYSQL_ASSOC
for the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array() originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array().

Resource:
mysql_fetch_assoc() [php.net]

As far as which control structure to use -- that will depend on how you want to control the flow of the logic in your script. The first iteration of a do-while [php.net] loop is going to run no matter what whereas a while [php.net] loop will run as long as the expression is true. You can always break [php.net] from either.

A switch doesn't loop through the result set, it's more like a bunch of

if
statements. If you only had one row being returned and wanted to check it against some values, the switch would be a control structure you might use, but not for looping through a result set.

StupidScript

7:00 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you, coopster.

The first iteration of a do-while loop is going to run no matter what whereas a while loop will run as long as the expression is true.

So the evaluation of whether the "while" condition is true falls after the first iteration of the "do...while" loop, whereas the evaluation of "while" happens before the first iteration of a "while" loop.

Gotcha. Thanks, again.

And performance characteristics are similar, depending on what is happening inside the loops? Or are they virtually interchangeable, more a matter of coding style than a practical matter?

mysql_fetch_array() and mysql_fetch_assoc()

If I need the indices, use fetch_array. If I don't, use fetch_assoc, which will use fewer resources. Yes?

IanKelley

8:47 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I forgot to mention you're only going to use less memory with method two if you're only fetching a numerically indexed array.

You can make fetch_array do the same as either fetch_assoc or fetch_row using the second parameter.

In other words there's no need for either function unless you think it makes your code more clear.

StupidScript

9:39 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you very much, Ian and coopster. I think I'm getting a handle on these issues. 'Ppreciate your time.