Jump to content


Photo

while loop, not working


  • Please log in to reply
6 replies to this topic

#1 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 28 February 2006 - 04:16 PM

this is the 2nd time ive had this bug, and cant figgure it out...

basicly its a while loop like this

$sql = mysql_query("SELECT * FROM members");
  while($members = mysql_fetch_array($sql))
  {

     ~random code~

   }

yet when i run this code, it only does 1 loop. even when there is more records to loop through.
i have put 'or die()' messagses on the end of the sql string, and nothing... i have even printed the data in the loop (thus how i know there is only 1 loop) so i tried doing this...

$sql = mysql_query("SELECT * FROM members");
  while($members = mysql_fetch_array($sql))
  {

     print $num;
     $num++;

   }

which shows numbers 1-(number of max records in my table)
so this shows the loop is working.. but only when my code sint inside it?

the only placves in my code inside this while loop is at sql queries, which have an error string inside the die() function, so that cant be why the code is stopping?

any way im lost to what could be wrong
Im not perfect ;) lol

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 February 2006 - 04:25 PM

Please tell me what this results in:
$sql = mysql_query("SELECT count(*) as COUNT FROM members");
  IF($members = mysql_fetch_array($sql))
  {

     echo $members['COUNT'];

   }


#3 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 28 February 2006 - 04:31 PM

[!--quoteo(post=350256:date=Feb 28 2006, 04:25 PM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Feb 28 2006, 04:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Please tell me what this results in:
$sql = mysql_query("SELECT count(*) as COUNT FROM members");
  IF($members = mysql_fetch_array($sql))
  {

     echo $members['COUNT'];

   }
[/quote]

i get 111
Im not perfect ;) lol

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 February 2006 - 06:24 PM

Please post the code that doesn't work. It sounds like you have a something in your code that is causing the loop to exit early. Maybe you're redefining the variable $members?

Ken

#5 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 February 2006 - 06:33 PM

Well, you should be getting that may row returns. Because you are not, you need to post your script, so we can see why it's not working...
[!--quoteo(post=350263:date=Feb 28 2006, 11:31 AM:name=leeming)--][div class=\'quotetop\']QUOTE(leeming @ Feb 28 2006, 11:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i get 111
[/quote]


#6 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 28 February 2006 - 06:47 PM

this is one of the 2 codes that dont work, but with the same problem... but this is the simplist to understand, without knowning the rest of my code

$hour and $minute are just quick ref for date(i) and date(H)

  $findnum1 = mysql_query("select * from revs WHERE min='$minute' AND hr='$hour'")or die('<B>ERROR</B>: Unknown error, array for rev number<BR>Please report this to an admin: '.mysql_error());
     while($findnum = mysql_fetch_array($findnum1))     //all members who are getting revived this min + hour
     {
       $heal = mysql_fetch_array(mysql_query("select * from member WHERE mid='$findnum[userid]'"));
       if($heal[hp]<$heal[max_hp])
       {
         mysql_query("update member set `hp`=`max_hp` WHERE mid='$heal[mid]'")or die('<B>ERROR</B>: Unknown error, update hp<BR>Please report this to an admin: '.mysql_error());
       }
       
       if($hour < "06" && $hour >= "00")
       { $starthour="06"; }
       elseif($hour < "12" && $hour >= "06")
       { $starthour="12"; }
       elseif($hour < "18" && $hour >= "12")
       { $starthour="18"; }
       elseif($hour >= "18")
       { $starthour="00"; }
       
       mysql_query("update revs set min='".rand(1,59)."', hr='".rand($starthour, ($starthour + 5))."' WHERE userid='$heal[mid]'")or die('<B>ERROR</B>: Unknown error, updating random revive timer<BR>Please report this to an admin: '.mysql_error());
       
     }

this is basicly a part of code for my game, it gives members full hp 4 times a day, but issues it at random times ( randomly between 00:00-06:00/ 06:00-12:00/12:00-18:00/18:00-00:00 , so basicly every 6 hours)
Im not perfect ;) lol

#7 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 03 March 2006 - 05:21 PM

i am still in need of help for this.
Im not perfect ;) lol




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users