Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Member
    Join Date
    Jan 2004
    Posts
    94
    Member #
    4729
    what's the best/fastest way to loop through the results of a query?

  2.  

  3. #2
    Senior Member ajaspers's Avatar
    Join Date
    Apr 2003
    Posts
    149
    Member #
    1150
    mysql_result() is the fastest but it only makes sense to use it for fetching a single cell from the DB.

    Otherwise:
    PHP Code:
    $query mysql_query("SELECT testid, col1, col2 FROM test"$db);

    while(
    $row mysql_fetch_array($queryMYSQL_ASSOC))
    {
      echo 
    "testid=$row[testid]\ncol1=$row[col1]\ncol2=$row[col2]\n\n";
    }

    mysql_free_result($query); 

  4. #3
    Senior Member teal's Avatar
    Join Date
    Dec 2003
    Posts
    247
    Member #
    4319
    I prefer
    PHP Code:
    $mysql_query mysql_query ('SELECT testid, col1, col2 FROM test'$db);

    for (
    $x 0$x mysql_num_rows ($mysql_query); $x++)
    {
      
    $assoc mysql_fetch_assoc ($mysql_query);
      print (
    "<p>$assoc[testid]</p>
        <p>
    $assoc[col1]</p>
        <p>
    $assoc[col2]</p>");

    Plus, that is really understandable, and can be done with tables or anything, depending on your creativity
    Whoth steps forth to mock the teal? Whom couldst say a color superior!

    Proclamith me, it cannot be done.

  5. #4
    Senior Member jlgosse's Avatar
    Join Date
    Jan 2004
    Location
    Newfoundland, Canada
    Posts
    1,037
    Member #
    4570
    Liked
    7 times
    $x = =;

    What you mean by that?


  6. #5
    Senior Member teal's Avatar
    Join Date
    Dec 2003
    Posts
    247
    Member #
    4319
    Called a typo, fixed it
    Whoth steps forth to mock the teal? Whom couldst say a color superior!

    Proclamith me, it cannot be done.

  7. #6
    Member macgruder's Avatar
    Join Date
    Apr 2004
    Location
    Japan
    Posts
    97
    Member #
    5524
    I too use the 'while' loop. I prefer it to the 'for' loop as it involves less code.

    mysql_fetch_array vs. mysql_fetch_assoc

    Pretty much use either one. The former is a superset of the later returning by default numerical and associative indices.

  8. #7
    Senior Member jlgosse's Avatar
    Join Date
    Jan 2004
    Location
    Newfoundland, Canada
    Posts
    1,037
    Member #
    4570
    Liked
    7 times
    Just making sure it wasn't something new to me. :P



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:04 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com