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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    while doesn't work

    Hi,
    I have a code that uses the While and it insert data to the datatable
    I want that while there is any name equal to that it wont insert any more data.

    However I can't make it work

    here is my code:
    Code:
      
    $resource = mysql_num_rows($sql);
    while ($resource <= 1){
    
    insertname();
    }
    I want to use while to loop through things untill a condition changes.
    How can I make it work?
    Last edited by devil_online; 02-14-2006 at 03:55 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try echoing resource to see what value it gets.
    $resource also needs to be incremented some how.
    It should probably be something like
    PHP Code:
    $resource mysql_num_rows($sql);
    $i 0;
    while (
    $i <= $resource){
    insertname();
    $i++;

    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Germany
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could it be that the amount of rows that get returned from the mysql_num_rows are higher than 1 ?

    If the condition of while isn't matched at the beginning, it gets skipped which would explain why it does not get executed.

    If you want it to loop trough the while at least once, you could use the do-while loop.

    I think the syntax was
    do {
    code..
    } while (condition );

    But yes, as _Aerospace_Eng_ said, you should echo it for debugging and see if everything goes the way you want; also you do need some kind of increment in the loop else chances are it turns to an endless loop

    - Felix

  • #4
    New Coder
    Join Date
    Jul 2003
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the problem is with the funtion
    here is the complet code

    Code:
     function insertname(){ 
    global $link;
    $firstnames = array("Jonh", "Paul", "Bill"); 
    $lastnames = array("Simon", "Luck","Guy"); 
    $name = $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)]; 
    $sql = mysql_query("SELECT names from home 
    WHERE names = '$name' 
    LIMIT 1") or die(mysql_error()); 
    $resource = mysql_num_rows($sql); 
    print $name; 
    $query = "INSERT INTO home(names) 
        values('$name')"; 
    mysql_query ( $query, $link) ;
    
    }
    
    
    while ($resource <= 1){
    
    insertname();
    }
    I don't want to increase the resourse. I want that the condition says while the resourse is not 0 it trys to create a new name

    thanks

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Did you even echo $resource to see what its returning? It could be that $resource is already greater than 1 so the while loop will never occur.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    New Coder
    Join Date
    Jul 2003
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no
    the resource is 0

    I think is because I've put the resourse inside the funtion.

    What do you think

    thanks
    Last edited by devil_online; 02-15-2006 at 03:45 AM.

  • #7
    New Coder
    Join Date
    Feb 2006
    Location
    Germany
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No variables are transfered between functions unless you tell them to be. ( or they are global )
    ..
    In this case resource doesn't seem to be global so try this:

    PHP Code:
     function insertname(){ 
    global 
    $link;
    $firstnames = array("Jonh""Paul""Bill"); 
    $lastnames = array("Simon""Luck","Guy"); 
    $name $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)]; 
    $sql mysql_query("SELECT names from home 
    WHERE names = '$name' 
    LIMIT 1"
    ) or die(mysql_error()); 
    $resource mysql_num_rows($sql); 
    print 
    $name
    $query "INSERT INTO home(names) 
        values('$name')"

    mysql_query $query$link) ;
    return (
    $resource);
    }

    $resource insertname();
    while (
    $resource <= 1){

    insertname();

    Although I am not sure if that works too well...

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this
    PHP Code:
    $resource mysql_num_rows($sql); 
    $i 0;
    function 
    insertname(){ 
    global 
    $link;
    $firstnames = array("Jonh""Paul""Bill"); 
    $lastnames = array("Simon""Luck","Guy"); 
    $name $firstnames[array_rand($firstnames)]." ".$lastnames[array_rand($lastnames)]; 
    $sql mysql_query("SELECT names from home 
    WHERE names = '$name' 
    LIMIT 1"
    ) or die(mysql_error()); 
    print 
    $name
    $query "INSERT INTO home(names) 
        values('$name')"

    mysql_query $query$link) ;
    }
    while (
    $i <= $resource){

    insertname();
    $i++;

    If resource is returning 0 then the loop will run one time. I don't think you want an infinite loop.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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