PC Nerd Posted April 2, 2007 Share Posted April 2, 2007 hi guys, its probably stupid, but i can fin the error in this line: while($Att_Start = mysqli_fetch_array($Att_Start_Query)) { this is the error: Warning: Invalid argument supplied for foreach() in C:\xampp\xampp\htdocs\Battle-Ages\Crons\cron_incs\Cron_Attack.inc on line 14 im receiving an endless loop with this error this is the entire code: <?php $Max_Points = $Page_Date['Points'] * 1.3; $Min_Points = $Page_Data['Points'] * 0.7; $Att_Start_SQL = "SELECT User_ID, Points FROM General_Stats WHERE Points > '".$Min_Points."' AND Points < '".$Max_Points."'"; $Att_Start_Query = mysqli_query($DB_Server, $Att_Start_SQL) or die("Could not retrive Attack Players."); while($Att_Start = mysqli_fetch_array($Att_Start_Query)) { $Att_Players[] = $Att_Start['User_ID']; } foreach($Att_Players as $Value){ echo $Value."<br>"; } ?> thanks for any help PC Nerd Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/ Share on other sites More sharing options...
papaface Posted April 2, 2007 Share Posted April 2, 2007 try: foreach($Att_Players as $key => $Value){ echo $Value."<br>"; } Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219691 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 nah soz, that doesnt work anyting else i should try? thanks Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219699 Share on other sites More sharing options...
MadTechie Posted April 2, 2007 Share Posted April 2, 2007 get ready to kick yourself replace while($Att_Start = mysqli_fetch_array($Att_Start_Query)) { with while($Att_Start == mysqli_fetch_array($Att_Start_Query)) { or even while(mysqli_fetch_array($Att_Start_Query)) { Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219702 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 um, == give a 60 second timout message and no other output = gives the output, looping endlessly for 60 seconds until the 60 second timeout thankx anyway, anything else? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219705 Share on other sites More sharing options...
MadTechie Posted April 2, 2007 Share Posted April 2, 2007 or even while(mysqli_fetch_array($Att_Start_Query)) { Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219710 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 looping with error output Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219711 Share on other sites More sharing options...
jitesh Posted April 2, 2007 Share Posted April 2, 2007 while($Att_Start = mysqli_fetch_array($Att_Start_Query)) { echo " <pre>"; print_r($Att_Start); // Are you getting array ? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219720 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 no sorry, still timeout anything else? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219724 Share on other sites More sharing options...
neel_basu Posted April 2, 2007 Share Posted April 2, 2007 Insert this just before while print_r($Att_Start);print_r($Att_Start_Query) Whats the output Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219730 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 dnt have time to expand, but is was with the quesy, and the incremet in Page_Data, not PageDate, i gtg thankx anyway Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-219734 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 does anyone have any ideas Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220100 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 get ready to kick yourself replace while($Att_Start = mysqli_fetch_array($Att_Start_Query)) { with while($Att_Start == mysqli_fetch_array($Att_Start_Query)) { or even while(mysqli_fetch_array($Att_Start_Query)) { Sorry techie your code isn't right, the $Att_Start = mysqli_fetch is correct, what this is doing is while the $Att_Start is true than keep the loop going and assign the value of mysqli_fetch_array to the variable. Now PC Nerd I have 2 options for you to try. 1: while($Att_Start = mysqli_fetch_array($Att_Start_Query) && !is_null($Att_Start)) { Or: $Att_Start = mysqli_fetch_array($Att_Start_Query) while(!is_null($Att_Start)) { //end of processing $Att_Start = mysqli_fetch_array($Att_Start_Query) } The 2nd should work, I am unsure about the first. Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220125 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 ok, i think ive solved it it was the origina file in shich this one was required...... there was a while loop that wasnt being correctly incremented. thankx anyway, ill post if i cant get it working Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220139 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 Yea while loops suck, also I am unsure how that mysqli_fetch works, but you may need to define it to return an associative index. see: http://us2.php.net/manual/en/function.mysqli-fetch-array.php for more information. Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220142 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 ok, i get some sort of output from my redefinine my array, with the while, in my oro=iginal code now when i want to display that... foreach($Att_Players as $Value){ echo $Value."<br>"; } i get: Warning: Invalid argument supplied for foreach() in FILE on line 18 whats happening i cant figure it out? thankx Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220151 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 Did you take a look into doing the mysqli_fetch_array($result, MYSQLI_ASSOC); Chances are that will work after reviewing the php man page on it. Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220153 Share on other sites More sharing options...
PC Nerd Posted April 2, 2007 Author Share Posted April 2, 2007 not 100 % sure what you mean i used the mysqli_fetch_array(); but i still got the same error its in the foreach loop in my original code, not thw while loop if i say echo $Att_Players, i get "Array" does this mean that its returning an object or somethign instread of the array values? thanks for any and all help, PC Nerd Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220158 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 Seriously dude, this reminds of something my grandma used to do to me when I was little, I used to say "Pion" instead of "Lion" and she would always go "Read my lips, LION" and I would be stubborn and say Pion. Anyhow try this: <?php $Max_Points = $Page_Date['Points'] * 1.3; $Min_Points = $Page_Data['Points'] * 0.7; $Att_Start_SQL = "SELECT User_ID, Points FROM General_Stats WHERE Points > '".$Min_Points."' AND Points < '".$Max_Points."'"; $Att_Start_Query = mysqli_query($DB_Server, $Att_Start_SQL) or die("Could not retrive Attack Players."); while($Att_Start = mysqli_fetch_array($Att_Start_Query, MYSQLI_ASSOC)) { $Att_Players[] = $Att_Start['User_ID']; } foreach($Att_Players as $Value){ echo $Value."<br>"; } ?> The issue is that mysqli_fetch_array returns a NON-ASSOCIATIVE array, meaning that $array['literalvalue'] does not work but $array[0] would. So you are creating an array with the $Att_Players[] line but nothing is being added to that array because there is no data due to the fact you did not specifiy the return of an associative array. You should probably read/look at the examples shown here: http://us2.php.net/manual/en/function.mysqli-fetch-array.php Read about arrays and associative arrays here: http://us2.php.net/manual/en/function.array.php Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220161 Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 actually my code is exactly what youhave as your example, and i still receive the error, now i have read that stuff and still cant see my error.. thankx for your help anuyting else? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220239 Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 anyting else i should try? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220375 Share on other sites More sharing options...
neel_basu Posted April 3, 2007 Share Posted April 3, 2007 Insert this just before while print_r($Att_Start); print_r($Att_Start_Query) Whats the output Show the Output Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220403 Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 Array mysqli_result Object ( ) 7 8 10 thats my output. im not sure about the numbers, but ithin its meant to be one of my randomly chosen user_ID's, but ill look at it, i dont kno about the rest...... thanks, what shounld i do??? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220427 Share on other sites More sharing options...
PC Nerd Posted April 3, 2007 Author Share Posted April 3, 2007 i refreshed the page, didnt change anyting, then received.... mysqli_result Object ( ) Warning: Invalid argument supplied for foreach() in FILE on line 20 anyting? Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220430 Share on other sites More sharing options...
neel_basu Posted April 3, 2007 Share Posted April 3, 2007 I am thinking but before taking any dicission. Add echo "\n".gettype($Att_Start); echo "\n".gettype($Att_Start_Query)."\n"; Just bellow the print_r() Add this Line at the top of your Page header("Content-Type: text/plain"); And Post the output of print_r(); Link to comment https://forums.phpfreaks.com/topic/45249-solved-foreach-loop-error/#findComment-220432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.