updating mysql rows in 'for' loop

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

updating mysql rows in 'for' loop

Post 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
dodga
Forum Newbie
Posts: 9
Joined: Sat Feb 07, 2004 2:10 pm
Location: Benztown, Germany

Re: updating mysql rows in 'for' loop

Post 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
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

Post 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;
Post Reply