Jump to content


Photo

Math problem


  • Please log in to reply
9 replies to this topic

#1 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 28 August 2006 - 01:17 PM

$var = 1;
mysql_query("UPDATE table SET var=var+'$var' FROM somewhere");

For some reason only Internet Explorer makes the math correct, other browsers always return a bit higher number than they should (ie 1+1 = 5). I'm quite lost...
I'm just a guy who likes to code for fun...

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 28 August 2006 - 01:56 PM

try it without the single quotes, since you're dealing with adding integers. as for the difference between browsers, that doesn't make much sense to me, since the math is being handled by the server. is the variable being populated from a form field, or when you run the code just as you have it, is it off? next question: why do you have a FROM clause in an update statment? what are you trying to do? shouldn't it be a WHERE?
<?php
$var = 1;
mysql_query("UPDATE tableName SET var = var + $var WHERE something = something");
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 28 August 2006 - 02:05 PM

Oops, your right about the from, I use where, I just wrote the code as an example and made a tiny mistake :)

No, the data isn't coming from a form, it's a number that is increased in a loop. But thanks, I'll try it without no single quotes. I can test it now though, as I'm at work, and all this comp has is IE...

To be more spesific, the number is a calculation actually, $var = round($var2 / $var3, 0);
I'm just a guy who likes to code for fun...

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 August 2006 - 10:23 PM

This can't have anything to do with the browser or MySQL... it's a PHP issue.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 29 August 2006 - 10:02 PM

This is getting funny:
$var = $var2 + 10;
mysql_query("UPDATE table SET var = $var WHERE some=thing ") or die('Query failed: ' . mysql_error());
echo $var;

the echo returns 16, but the update makes it 18... And though it seems extremely odd, IE just makes it right, and firefox doesn't. So much for it being a serverside language ;)

$var2 = -1 * round(($var3 * $var4) / $var5, 0);

Oh and with  or without ' ' seems to make no diffrence...
I'm just a guy who likes to code for fun...

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 August 2006 - 10:37 PM

I agree with Fenway.
Legend has it that reading the manual never killed anyone.
My site

#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 August 2006 - 11:33 PM

Well, at least in theory, you're not locking your tables, so you could have a race condition somewhere.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#8 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 August 2006 - 11:45 AM

I'm somewhat new to mysql, what do you mean by locked?
I'm just a guy who likes to code for fun...

#9 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 31 August 2006 - 01:48 PM

There can be a concurrency issue with non-transaction safe statements.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#10 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 August 2006 - 01:49 PM

Ah, that makes sense :)
I'm just a guy who likes to code for fun...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users