I have the following code:
while(1){
$r = rand(0,9);
echo "running...";
if($current_item >= $array[$r]['limiter']){
$foo = $foo;
$bar = $bar;
mysql_query("INSERT INTO db (type, hp) VALUES ('$foo', '$bar')");
echo "done.";
break 1;
}
}
When I run this I'll get "running..."
echoed out once or a few times (which is expected) and "done."
echoed out once, as expected.
Yet two entries will be inserted into the database with different information hinting that the entire if statement ran twice.
Has anybody run into this before? Is there something happening that I'm not seeing here? Any ideas how to fix this?
I have a similar problem with Updates, they too update twice and they're completely separate from a loop.
Edit: just to try to clarify,
$current_item
does change but in the context of the loop it is constant.
$array[$r]['limiter']
references a multidimensional array.
Neither of these should affect the loop itself, it's just a condition that has to be met to do anything.
I modified a little the code in order to be able to run it, the code I ran is:
for ($r=0;$r<10;++$r) {
$array[$r]['limiter'] = $r;
}
$current_item = 3;
$foo = 1;
$bar = 2;
while(1){
$r = rand(0,9);
echo "running...";
if($current_item >= $array[$r]['limiter']){
$foo = $foo;
$bar = $bar;
echo 'do_query: '."INSERT INTO db (type, hp) VALUES ('$foo', '$bar')";
echo "done.";
break 1;
}
And the responce:
1st time: running...do_query: INSERT INTO db (type, hp) VALUES ('1', '2')done.
2nd: running...running...running...do_query: INSERT INTO db (type, hp) VALUES ('1', '2')done.
3rd: running...running...do_query: INSERT INTO db (type, hp) VALUES ('1', '2')done.
So the query executes only once with this code. The problem must be in a piece of code before the while, so it get into the while 2 times.