robert_gsfame Posted May 15, 2010 Share Posted May 15, 2010 back to previous loop problem, i have this code...but i am not sure why it takes time to load the page <?php require_once('config.php'); $username="James"; $checkfirstuser=mysql_query(sprintf("SELECT * FROM test WHERE username='%s'", mysql_real_escape_string($username))); $i=1; $tot=0; while($i==1){ $rand=rand(0,9); while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){ $number=$checkfirstuserarray['id']; if($rand==$number){ $tot++;} } if($tot==0){ $i++;}else{} } echo $rand; ?> Quote Link to comment Share on other sites More sharing options...
-Karl- Posted May 15, 2010 Share Posted May 15, 2010 "Load takes times". Please be more descriptive. Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 i got the warning load exceeds limit blabla 60 seconds like that... is there something wrong with the code especially with the loop or if anyone could come up with simple one thx Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 Maximum Execution Time Of 60 Seconds Exceeded.. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 You're loop is similar to the one I rewrote in your other post. Instead of <?php $i=1; $tot=0; while($i==1){ $rand=rand(0,9); while($checkfirstuserarray=mysql_fetch_array($checkfirstuser)){ $number=$checkfirstuserarray['id']; if($rand==$number){ $tot++;} } if($tot==0){ $i++;}else{} } ?> Try <?php $checkfirstuserarray=mysql_fetch_assoc($checkfirstuser); $number=$checkfirstuserarray['id']; $rand = rand(0,9); while ($rand == $number) { $rand = rand(0,9); } echo $rand; ?> BTW, get into the habit of properly indenting your code, it makes reading & debugging code much easier. Also you don't need an "else" clause if there is nothing in it. Ken Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 i try the code but the result is not correct i have 3 and 5 inside my database and when using your code, 5 still appear any loop missing?? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 What do you mean by 3 & 5 inside? Two rows? One row containing two number? Ken Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 nope...two rows James 3 James 5 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 Ok, do this: <?php $tmp = array(); while($checkfirstuserarray=mysql_fetch_assoc($checkfirstuser)) { $tmp[] = $checkfirstuserarray['id']; } $rand = rand(0,9); while (!in_array($rand,$tmp) { $rand = rand(0,9); } echo $rand; ?> Ken Quote Link to comment Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 Cool!!!Thx kenrbnsn!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.