Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.