Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Location
    Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP efficiency/server load question

    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?
    Code:
    while ($loop < $numrows) {
    $html .= mysql_result($result,$loop,"fieldname");
    $loop++;
    }
    echo $html;
    or

    Code:
    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

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    for ($i 0$i $numrows$i++) {
    echo 
    mysql_result($result,$i,"fieldname");

    Just tossing in another option.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

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

    echo 
    $html
    or

    PHP Code:
    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.

  • #4
    New to the CF scene
    Join Date
    Jun 2005
    Location
    Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by owlpages
    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?

  • #6
    New to the CF scene
    Join Date
    Jun 2005
    Location
    Australia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, i see, thanks for that!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •