Jump to content

Go through a table in the database and chance all value=0 to value=3


steve_683

Recommended Posts

This is quite a novice question, and hopefully one with an easy answer.

 

I want to go through a MySQL table on my server and change value=`0` to value=`3` everywhere where attributeid==`2` (and value==`0`). Here`s what the table looks like:

 

| attributeid | userid | value |

-----------------------------

|  1          |    1    |    1    |

|  2          |    1    |    0    |

|  3          |    1    |    45  |

|  1          |    2    |    1    |

|  2          |    2    |    4    |

...and so on

 

So I guess I run the following code:

 

$newvalue = 3;

mysql_query("UPDATE name_of_table SET value='$newvalue' WHERE (value=='0' AND attributeid=='2')");

 

Is this going to work, or am I potentially going to screw-up something? I'm a very novice programmer, and I'm very worried I'm going to make some kind of serious mistake.

Testing code involves actually trying something and seeing if the results are what you expect, this is why you run tests on a development system where the data either does not matter or you have a backup of it that you can easily restore if something goes wrong.

 

Edit: following is basically the same as posted above -

 

The only thing you need to change is the double == signs should be single =

 

In SQL a comparison is only one = sign.

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.