Page 1 of 1

updating mysql rows in 'for' loop

Posted: Sat Feb 07, 2004 2:43 pm
by dsdsdsdsd
hello;

Code: Select all

<?php
$query = "update $table set caption='l' where(item_number='{$i}')";
 
for ($i=1 ; $i<=8 ; $i++)
    {if (mysql_db_query($database , $query , $link))
        {echo "&doppie=good&";
        }
    };

?>
my problem, I believe, is the syntax of

Code: Select all

<?php
...number='{$i}')"; 
?>
any thoughts?
thanks,
Shannon Burnett

Re: updating mysql rows in 'for' loop

Posted: Sat Feb 07, 2004 2:49 pm
by dodga
Actually I think that PHP evaluates $i as soon, as it appears. So when it parses the line

Code: Select all

$query = "update $table set caption='l' where(item_number='{$i}')";
it evaluates $i to whatever it is set before that line.
Try echo'ing $query to prove that.

What you might try is using sprintf():

Code: Select all

<?php
$rawQuery = "update $table set caption='l' where (item_number='%d')";
 
for ($i=1 ; $i<=8 ; $i++) {
  $query = sprintf($rawQuery, $i);
  if (mysql_db_query($database , $query , $link)) {
    echo "&doppie=good&";
    }
}
?>
HTH

Posted: Sat Feb 07, 2004 4:13 pm
by dsdsdsdsd
hello dodga;

" it evaluates $i to whatever it is set before that line "

this is an interesting point; in my case $i did not have a value yet or was even defined; when I echo'd the $query I got all the expected stuff: blah ... _number='')
so you are right;

solution: I just moved the $query = ...; inside the loop;