Page 1 of 1
A better way to do arithmetic stuff.
Posted: Sat Feb 24, 2007 10:35 pm
by Mightywayne
Hey. I wanna add, subtract, multiply, w/e, using SQL on a PHP script. So it's like.
Code: Select all
mysql_query("UPDATE user SET hat +1 WHERE username whatever whatver");
That's just a silly example. Anyway, so that would make whatever's in the "hat" entry +1. OR SO I THOUGHT! Apparently it just... doesn't work like that. I can't find the tutorial on Google about it, and I've got a feeling it's not even UPDATE. But it's definitely not like SUM or anything.
I think what I'll have to do is just set like. $hat = $hat +1
And then be like.
But I really wouldn't like to do that. Is there another option or what?
Posted: Sat Feb 24, 2007 10:38 pm
by volka
try
Code: Select all
$query = "UPDATE user SET hat=hat+1 WHERE username whatever whatver";
mysql_query($query) or die(mysql_error().': '.$query);
Posted: Sun Feb 25, 2007 8:29 am
by Mightywayne
Oh, I remember now. I tried that, and that was actually the only one that worked. Addition. I'm trying to subtract now, and multiplying and dividing would be nice to know, too.
Posted: Sun Feb 25, 2007 4:21 pm
by volka
Code: Select all
UPDATE user SET hat=hat+1 WHERE username whatever whatver
UPDATE user SET hat=hat-1 WHERE username whatever whatver
UPDATE user SET hat=hat*1 WHERE username whatever whatver
UPDATE user SET hat=hat/1 WHERE username whatever whatver
Posted: Sun Feb 25, 2007 5:01 pm
by Mightywayne
Ruff! I've tried that already!
Code: Select all
if ($theirmoney <= '2000')
mysql_query("UPDATE user SET money=money-2000 WHERE username = '$user'");
die("onfg ");}
!!! Doesn't work!
Posted: Sun Feb 25, 2007 5:13 pm
by onion2k
Mightywayne wrote:Ruff! I've tried that already!
Code: Select all
if ($theirmoney <= '2000')
mysql_query("UPDATE user SET money=money-2000 WHERE username = '$user'");
die("onfg ");}
!!! Doesn't work!
Define "doesn't work". Do you mean "it dies with my 'onfg lol' message"? That'll happen because the die isn't dependent on the SQL query.
Code: Select all
if ($theirmoney <= '2000') {
mysql_query("UPDATE user SET money=money-2000 WHERE username = '$user'") or die("onfg ");
}
A word of advice too, make your die() messages something meaningful. die( "SQL Query failed: ".mysql_error()) will help you find problems much quicker than 'onfg lol'.
Posted: Sun Feb 25, 2007 5:56 pm
by Mightywayne
Ohhhhhhh! But I thought it would like, do that stuff, THEN die. That's weird to me. But if that's what happens, then I guess that's what happens. It's okay though, I did the die() to not continue anything after that. I suppose now maybe the mysql error would have helped me, but usually I just type in j89jdf3 and see if it'll do what I want to the script, so I don't put any effort into error checking.
Thanks for the help! I check it right now, though I'm sure it'll work fine.
Posted: Sun Feb 25, 2007 7:59 pm
by volka
Mightywayne wrote:do that stuff, THEN die
What made you think that?
OR evaluatues the operand step by step from left to right. As soon as one returns true (or "not false") php stops evaluating the operands
e.g.
Code: Select all
$a = true or false; // php doesn't evaluate false
$a = a() or b(); // if a() returns "not false" php doesn't call b()
$a = mysql_query() or die(mysql_error()); // if mysql_query() returns "not false" php doesn't call die(mysql_error())
mysql_query returns
false if an error occured.
Mightywayne wrote:I suppose now maybe the mysql error would have helped me, but usually I just type in j89jdf3 and see if it'll do what I want to the scrip
No error handling at all will only slow you down.