A better way to do arithmetic stuff.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

A better way to do arithmetic stuff.

Post 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.

Code: Select all

UPDATE user SET hat = $hat
But I really wouldn't like to do that. Is there another option or what?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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'.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply