go through each column in table and update

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

go through each column in table and update

Post by ecaandrew »

I am trying to figure out how to do something with php.
basically i have a table with the fields user_id,friend_id,new

i want it to to go through each row where USER_ID = 5 and update the new field to "1"


so far i have something like this

Code: Select all

if ($row_profile_count > 0) {
$sql = mysql_query("select * from friends where userid='".$_SESSION['session_id']."'");


i know it should be a foreach statement i think


}
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

why not just

Code: Select all

mysql_query("UPDATE friends SET new=1 WHERE USER_ID=5");
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

Post by ecaandrew »

because there will be multiple rows

it will need to be like this

Code: Select all

UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='1'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='2'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='3'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='4'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='5'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='6'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='7'
UPDATE friends SET new='1' WHERE userid='USER_ID' AND friendid='8'
its going to need to go through a series depending how many are in the database, i know it should be a foreach
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

above what i wrote should update all that have user_id 5.
Post Reply