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; ?> Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/ Share on other sites More sharing options...
-Karl- Posted May 15, 2010 Share Posted May 15, 2010 "Load takes times". Please be more descriptive. Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058772 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 Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058774 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.. Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058777 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 Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058791 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?? Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058796 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 Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058804 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 Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058805 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 Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058807 Share on other sites More sharing options...
robert_gsfame Posted May 15, 2010 Author Share Posted May 15, 2010 Cool!!!Thx kenrbnsn!! Link to comment https://forums.phpfreaks.com/topic/201871-load-takes-time/#findComment-1058812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.