...

View Full Version : PHP efficiency/server load question



owlpages
06-29-2005, 01:57 AM
Hello folks, i am working on my first php/MySQL project, and i am concerned about server loads.
which of the following methods would be more efficient?


while ($loop < $numrows) {
$html .= mysql_result($result,$loop,"fieldname");
$loop++;
}
echo $html;


or



while ($loop < $numrows) {
echo mysql_result($result,$loop,"fieldname");
$loop++;
}


i have used the first method because of the template system i devised, but looking at it, i am concerned that the $html variable may get too large.

Thanks,
Deane

Kurashu
06-29-2005, 02:02 AM
for ($i = 0; $i < $numrows; $i++) {
echo mysql_result($result,$i,"fieldname");
}


Just tossing in another option.

missing-score
06-29-2005, 02:08 AM
to be honest, i doubt you would be able to find any efficiency difference between them.

The main point for improvement that I can see is that mysql_result() is not a 'favoured' function to use. Here is what PHP.net say:


When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

I would use:



$html = '';
while ($row = mysql_fetch_assoc($result)) {
$html .= $row['fieldname'];
}

echo $html;


or



while ($row = mysql_fetch_assoc($result)) {
echo $row['fieldname'];
}


Whether you use the echo within the while loop or not should depend on whether you need to... Do you need to get the results returned by mysql_fetch_assoc() and then print them out somewhere else, or use them more than once? if that is the case then definately assign them a variable, but if not then print them out right away. To be honest, i really dont think you would notice a server load difference unless you are working with datasets containing hundred-thousand to millions of rows... and even then i doubt either way would be much different in terms of efficiency.

owlpages
06-29-2005, 03:23 AM
Hi guys, thanks for your replies!

my largest table only has 783 rows and 17 columns, so i guess the effeciency is nothing to worry about then.

I'll try the mysql_fetch_assoc, but i don't completely understand the while loop you've used in your example as there's no loop increment going on.

the reason i am assigning the variable is the template i am using, it basically goes:
1 - do all the php/MySQL processing, assigning all the variable such $PageTitle $html etc
2 - include the template page.php, which is a html page with <?=$PageTitle?> and <?=$html?> etc in the appropriate places.

Kurashu
06-29-2005, 04:49 AM
I'll try the mysql_fetch_assoc, but i don't completely understand the while loop you've used in your example as there's no loop increment going on.

There is, just in the background. And let me explain it to you. It is going set $row equal to the results returned by the query. If it is true, then the loop goes on to do what is contained in it. Then PHP increases the row number, and does the loop again. When $row ends up false, the loop cancels and moves on to the next block of code.

Make sense?

owlpages
06-29-2005, 05:49 AM
Ah, i see, thanks for that! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum