- Impact
- 15
How will I go about minus 1 to an entire column in a table?
-S.
-S.
<?php
$db = mysql_connect("localhost", "hostname_username", "password");
mysql_select_db(hostname_table", $db);
$query = mysql_query("SELECT * FROM table", $db);
while ($col = mysql_fetch_array($query)) {
mysql_query("UPDATE table SET column = '$col[num] - 1' WHERE column = '$col[num]'", $db);
}
?>
Palyriot said:If every row is the same in that column, then that will work, but if they are different at all, you need a loop. You didn't really clarify the contents of it.
Palyriot said:Doesn't he want to do it to ever row in his table? That's what he makes it sound like when he says "to an entire column". If you want to do it to every row in one column, then you need a loop. If there is only one row needed to be changed, then it is just one mysql query and he is done.
MikeBigg said:Palyriot said:If every row is the same in that column, then that will work, but if they are different at all, you need a loop. You didn't really clarify the contents of it.
Palyriot,
with respect, this can be done in SQL ... no php and no loop required ... the database engine will take care of all the looping for you.
Look at it a different way. If you wanted to decrement a single value in a column you would use something like:
UPDATE <table> set <column> = <column> - 1 where id = 77
dropping off the where clause causes the UPDATE statement to act on all rows in the table one by one.
Mike
Palyriot said:Wow, I'm an idiot. I'm sorry.
. . .
I'm really sorry I didn't see that the entire time. It was so simple. That's really embarresing.![]()