Jump to content

[SOLVED] SQL while mass looping


Q695

Recommended Posts

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)";
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/152000-solved-sql-while-mass-looping/
Share on other sites

  • 3 months later...

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.