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 4 of 4
  1. #1
    Junior Member
    Join Date
    Dec 2004
    Posts
    5
    Member #
    8447
    Hi all,

    i have a question about the performance regarding the following when obtaining a recordset:

    eg.
    1) SELECT * FROM student WHERE student id IN (1,2,3,4,5,6);

    or

    2) for ($i=1; $i < 7; $i++){
    $query = " SELECT * FROM student where student id = $i"
    mysql_query ($query);
    }

    both does the same thing but i want to know which one is the better approach if there's a difference. Im grateful to any1's feedback on this.

    regards,
    Sing

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    By far, the first method is preferable. Never query in a loop.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Junior Member
    Join Date
    Dec 2004
    Posts
    5
    Member #
    8447
    Quote Originally Posted by filburt1
    By far, the first method is preferable. Never query in a loop.

    Thanks for the fast response!!
    i'll use the first method then

    Cheers,
    Sing

  5. #4
    Senior Member Stylise's Avatar
    Join Date
    Jul 2005
    Location
    Mount Martha, Australia
    Posts
    229
    Member #
    10679
    Not knowing what the actually practical use the eg is based on, you could also use LIMIT for that eg.

    Code:
    SELECT * FROM student ORDER BY student_id ASC LIMIT 0, 6;
    or a better loop one be:

    PHP Code:
    for ($i=1$i 7$i++){
        if (
    $i >1)
            
    $idValues .= ",";
        
    $idValues .= $i;
    }

    $query " SELECT * FROM student WHERE student_id = '$idValues'";
    mysql_query ($query); 


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 04:29 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com