Q695 Posted April 1, 2009 Share Posted April 1, 2009 Why doesn't this work, even though all the SQL statements are returning data successfully? <?php //////////////////////////////////////////////////////////// level regeneration $sql="SELECT * FROM level_settings WHERE regen_time='1';"; $result=@mysql_query($sql,$con) or die(death($sql)); $row=mysql_fetch_array($result); print_r($row); while ($row=mysql_fetch_array($result)){ $regen_time=$row[regen_time]; $regen_amt=$row[regen_ammount]; $init_loc=$row[init_loc]; $sqll="SELECT * FROM map WHERE lvl='$row[level]';"; $resultl=@mysql_query($sqll,$con) or die(death($sqll)); $rowl=mysql_fetch_array($resultl); print_r($rowl); $location_num=mysql_num_rows($resultl); /////////////////////////////////////////////////////////// regeneration info $sql2="SELECT * FROM level_settings WHERE regen_time='1';"; $result2=@mysql_query($sql2,$con) or die(death($sql2)); while ($row2=mysql_fetch_array($result2)){ for ( $counter=1; $counter<=$row2[regen_ammount]; $counter+=1) { ////////////////////////////////////////////////////////////// create creatures echo "num rand(1, $location_num)"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/ Share on other sites More sharing options...
corbin Posted April 1, 2009 Share Posted April 1, 2009 What do you mean why doesn't it work? You probably want to do: echo "num " . rand(1, $location_num); If I had to guess. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-798276 Share on other sites More sharing options...
Q695 Posted April 1, 2009 Author Share Posted April 1, 2009 I noticed I was calling 2 $row s. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-798358 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 No offense, but you should also consider formatting your code, it's nearly impossible to read and will make detecting errors a whole lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-798361 Share on other sites More sharing options...
Q695 Posted April 1, 2009 Author Share Posted April 1, 2009 I do format it well into logical functions. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-798393 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 I meant as far as indentation and spacing. It's very poorly formatted in that sense. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-798579 Share on other sites More sharing options...
haku Posted April 6, 2009 Share Posted April 6, 2009 ; $result=@mysql_query($sql,$con) or die(death($sql)); You have to decide if you want to suppress the errors or not - you are doing both in this code. It makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-801959 Share on other sites More sharing options...
Psycho Posted July 16, 2009 Share Posted July 16, 2009 A couple things: 1, You are "throwing out" the first records from each of the first two queries. You do a mysql_fetch_array() and do a print_r() on the first record, then you do a while loop with a mysql_fetch_array() for each record after that - in other words the while loop starts on the second record. If either of the first two queries return only one record then the while loops will not run. 2. Why three queries when you could do it with one? What really makes no sense is that these are all within loops and the third query is exactly the same as the first. Edit: One other thing, you are not referencing your query array results correctly, so all of your values are probably 0. WRONG: $regen_time=$row[regen_time]; RIGHT: $regen_time=$row['regen_time']; (notice the quote marks) Unless you had defined 'regen_time' as a constant with a value the same as a field name then you are referencing any arary index that does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/#findComment-876282 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.