tags:

views:

89

answers:

2

i have a TABLE named FLY,i have a column in it named FLY_NUM.im trying to decrease the value of this column in a mysql query(in PHP). something like this :

mysql_query("UPDATE fly SET  `fly_Num` = `fly_Num` -1 WHERE fly_ID ='1' ");

this is wrong!everytime this query run,FLY_NUM column set to zero.how can i do such a thing?

+5  A: 

Your query is valid and correct. There is also no need to put a space between the - and the 1.

Test case:

CREATE TABLE fly (fly_id int, fly_num int);

INSERT INTO fly VALUES (1, 1);
INSERT INTO fly VALUES (1, 2);
INSERT INTO fly VALUES (1, 3);
INSERT INTO fly VALUES (1, 4);
INSERT INTO fly VALUES (1, 5);
INSERT INTO fly VALUES (1, 6);
INSERT INTO fly VALUES (1, 7);

Update Query:

UPDATE fly SET `fly_Num` = `fly_Num` -1 WHERE fly_id ='1';

Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0

New table contents:

SELECT * FROM fly;

+--------+---------+
| fly_id | fly_num |
+--------+---------+
|      1 |       0 |
|      1 |       1 |
|      1 |       2 |
|      1 |       3 |
|      1 |       4 |
|      1 |       5 |
|      1 |       6 |
+--------+---------+
7 rows in set (0.00 sec)

It works even if you use a varchar for the fly_num column:

CREATE TABLE fly (fly_id int, fly_num varchar(10));

INSERT INTO fly VALUES (1, '1');
INSERT INTO fly VALUES (1, '2');
INSERT INTO fly VALUES (1, '3');
INSERT INTO fly VALUES (1, '4');
INSERT INTO fly VALUES (1, '5');
INSERT INTO fly VALUES (1, '6');
INSERT INTO fly VALUES (1, '7');

Update Query:

UPDATE fly SET `fly_Num` = `fly_Num` -1 WHERE fly_id ='1';

Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0

New table contents:

SELECT * FROM fly;

+--------+---------+
| fly_id | fly_num |
+--------+---------+
|      1 |       0 |
|      1 |       1 |
|      1 |       2 |
|      1 |       3 |
|      1 |       4 |
|      1 |       5 |
|      1 |       6 |
+--------+---------+
7 rows in set (0.00 sec)
Daniel Vassallo
Can you elaborate? Notice that fly_Num and fly_ID are not the same.
Sjoerd
@Sjoerd: You're right, I didn't notice. I've updated my answer.
Daniel Vassallo
yes buddy,i did it too.i run it as a sql query in phpmyadmin and it works fine,but in my php query it will set column to zero!!!!!!i copied the exact query in my codes!what is problem?!
armin etemadi
Very strange. Let's see if someone comes with any new ideas... I can't think of any :)
Daniel Vassallo
thank you so much :)
armin etemadi
i discovered new thing about this problem,everytime this query runs in my php code,it will decrease column by 3!!!!!!!i tried my query as this : "... `fly_Num` = `fly_Num` -3 ..."and my value decreased 9 times.it means if i had 20 in this column,it changed to 11
armin etemadi
@armin: That really looks like your php code is being executed x3 times.
Daniel Vassallo
no Daniel.i had run it in single php file with no extra codes!!!!but it still wrong!
armin etemadi
+1  A: 

The problem is in your PHP code.

Are you running this in a loop? Another way of asking this would be do you do anything special in you code when fly_num = 0? Phpmyadmin uses the same mysql connector as you code so the problem must be in your code not in your query.

NOTE: Another possible problem in your PHP is that you're including the file multiple times.

Every time its included it will call the database and decrement by 1.

Wes
no,i tried it in a single file,that just doing this query!but the problem is still!!!i discovered new thing about this problem,everytime this query runs in my php code,it will decrease column by 3!!!!!!!i tried my query as this : "... `fly_Num` = `fly_Num` -3 ..."and my value decreased 9 times.it means if i had 20 in this column,it changed to 11 !!!!!!!!!!!!!!!!my mind is blowing!!!
armin etemadi
I've updated my answer because I think that this may be easier to understand.
Wes
@armin: that obviously means you are calling the update three times per id
knittl