+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Loops

  1. #1
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    Loops

    $result1 = mysql_query("SELECT * FROM people WHERE id = '$number[0]'") or die(mysql_error());
    $row1 = mysql_fetch_array( $result1 );
    $result2 = mysql_query("SELECT * FROM people WHERE id = '$number[1]'") or die(mysql_error());
    $row2 = mysql_fetch_array( $result2 );
    $result3 = mysql_query("SELECT * FROM people WHERE id = '$number[2]'") or die(mysql_error());
    $row3 = mysql_fetch_array( $result3 );
    $result4 = mysql_query("SELECT * FROM people WHERE id = '$number[3]'") or die(mysql_error());
    $row4= mysql_fetch_array( $result4 );
    $result5 = mysql_query("SELECT * FROM people WHERE id = '$number[4]'") or die(mysql_error());
    $row5 = mysql_fetch_array( $result5 );

    echo $row1[answer];
    echo $row2[answer];
    echo $row3[answer];
    echo $row4[answer];
    echo $row5[answer];



    Is there anyway I can put this code into a loop?
    Last edited by taekwondokid42; 11-21-2007 at 05:22 PM. Reason: syntax error.

  2. #2
    Slothie's Avatar
    Slothie is offline Lord Of The Keys Slothie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Singapore
    Posts
    1,432

    Re: Loops

    PHP Code:
    $result mysql_query("SELECT * FROM people WHERE id in  ($number[1],$number[2],$number[3],$number[4],$number[5]) ") or die(mysql_error()); 

    while (
    row5 mysql_fetch_array$array)) {
    echo 
    $row[$answer];



    Easiest 70 points you'll make on x10

    Feel free to add my reputation by clicking on the if you found my post helpful to you :P


    If I am not responding to your PMs, that means I am ignoring you. Take a hint.



    09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0


  3. #3
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    Re: Loops

    thank you.

  4. #4
    rickle42 is offline x10Hosting Member rickle42 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    59

    Re: Loops

    $temp=0;
    while(echo mysql_query("SELECT answer FROM people WHERE
    id = '$number[$temp++]'")){echo '\n';}


    does this work?
    Last edited by rickle42; 11-22-2007 at 02:42 AM. Reason: typo

  5. #5
    Slothie's Avatar
    Slothie is offline Lord Of The Keys Slothie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Singapore
    Posts
    1,432

    Re: Loops

    You can't echo a mysql_query directly and by the logic, it'd work if the numbers are sequential.

    Easiest 70 points you'll make on x10

    Feel free to add my reputation by clicking on the if you found my post helpful to you :P


    If I am not responding to your PMs, that means I am ignoring you. Take a hint.



    09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0


  6. #6
    flinx is offline x10Hosting Member flinx is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    68

    Re: Loops

    Quote Originally Posted by rickle42 View Post
    $temp=0;
    while(echo mysql_query("SELECT answer FROM people WHERE
    id = '$number[$temp++]'")){echo '\n';}


    does this work?
    No it doesn't work.

  7. #7
    Thewinator is offline x10 Lieutenant Thewinator is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    [NL]
    Posts
    256

    Re: Loops

    Quote Originally Posted by Slothie View Post
    PHP Code:
    ...
    while (
    row5 mysql_fetch_array$array)) {
    ... 

    Isn't that supposed to be

    PHP Code:
    ...
    while (
    $row mysql_fetch_array$array)) {
    ... 
    And another loop could be
    PHP Code:
    for($iIndex=0$iIndex<5$iIndex++)
    {
    $result{$iIndex} = mysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
    $row{$iIndex} = mysql_fetch_array$result{$iIndex} );
    echo 
    $row{$iIndex}[answer];

    Last edited by Thewinator; 11-22-2007 at 09:36 AM.



  8. #8
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: Loops

    2 questions based on the recent loops that've gone through actually:

    1) Is it faster to do everything in one query then sort it out via php code (read: in this case, grab everyone and sort by id, then run a loop via php to check all the people and see if the id's match)?

    2) Is it less intensive on server resources to grab it all, then sort via php code?


    Asking cause if either one is true, then theres no reason to run 5 separate queries. It's not necesarilly gunna kill a small site, but if you have 100 users an hour (just an example), you can either do 100 queries with some php code to sort the results how you want, or you can do 500 to do 5 queries a user.

    I mean, again, not a big difference. But say a query takes 0.1 seconds (thats 1/10th if you can't see the decimal); that's 10 seconds if you do 1 query a user, or 50 if you do 5. That'd be enough to slow things down a bit, especially since I doubt those'd be the ONLY 5 queries done (one particular page on my site is running 7, but I'm working out how to condense that).


    Just curious. More cause if you do anything where it's gunna be heavilly used, it's obviously better to make it take the least time possible (so server resource usage script doesn't hit you, cause it isn't lasting long enough to make the script mad), and to use as few resources as possible (again, so the SRU script doesn't hit you until you get way too many members).


    My guess? My guess is it's less intensive to grab it once, but it might not show up with a small DB.

    My reasoning for this: with each query, wouldn't MySQL have to go through the entire database AGAIN? I'd rather just send it through the DB once with a single query and get all the main results, then go through it with php and check the 5 values ($number[0], $number[1], etc) rather than have to send MySQL through the DB a second, third, fourth, and fifth time to get more data.


    -=Livewire's Crappy Analogy Time=-
    You don't go to the store to buy the cake mix, come home, put it away, then go back and get the eggs, come home, and put it away, then go back and get the milk, come home, put it away, then go back to get the candles, come home, put it away, then go get the matches, come home, and put them away.

    You go to the store and buy the cake mix, eggs, milk, candles, and the matches, then you sort it out when you get home.



    Again, dunno if I'm even right. I'd just think it'd be easier to grab it in one shot then let php sort it out, rather than make MySQL sift a possibly-10,000 entry database 5 times. I HIGHLY doubt it'd make a difference in a small DB (having mysql go through 5 lines probably takes about as long as going through 6, so it doesn't matter that much on something small), but a large one for a popular forum?


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  9. #9
    Slothie's Avatar
    Slothie is offline Lord Of The Keys Slothie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Singapore
    Posts
    1,432

    Re: Loops

    Its less intensive to do it in MySQL, after all that's what databases are for, for you to manipulate data as you see fit.

    @thewin
    PHP Code:
    for($iIndex=0$iIndex<5$iIndex++)
    {
    $result{$iIndex} = mysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
    $row{$iIndex} = mysql_fetch_array$result{$iIndex} );
    echo 
    $row{$iIndex}[answer];

    That'd work assuming its sequential data he wanted. It could be shortened to
    PHP Code:
    for($iIndex=0$iIndex<5$iIndex++)
    {
    $resultmysql_query("SELECT * FROM people WHERE id = '$number[$iIndex]'") or die(mysql_error()); 
    $row mysql_fetch_array$result );
    echo 
    $row[answer];

    So you wouldn't have to create 5 different values in memory as you only need the current one. Not much difference for 5 records but for 500? 5000? Notice that yours also queries the database 1 time for each record, if he needed 500 records that'd be 500 queries.

    Looks like a red flag from x10's resources watchdogs :P

    Easiest 70 points you'll make on x10

    Feel free to add my reputation by clicking on the if you found my post helpful to you :P


    If I am not responding to your PMs, that means I am ignoring you. Take a hint.



    09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0


  10. #10
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: Loops

    Making sure I read it properly; it's easier to sort it in the sql database, but at the same time it's a BAD idea to use 500 to build the page? :P

    I'll keep that in mind. I still need to shorten my 7 query page down though, it shouldn't need 7 to display the news >_<


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Intro To C++
    By wizeman in forum Tutorials
    Replies: 7
    Last Post: 06-24-2005, 04:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers