Jump to content

Archived

This topic is now archived and is closed to further replies.

Ifa

Math problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?
[code]
<?php
$var = 1;
mysql_query("UPDATE tableName SET var = var + $var WHERE something = something");
?>
[/code]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
This can't have anything to do with the browser or MySQL... it's a PHP issue.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Well, at least in theory, you're not locking your tables, so you could have a race condition somewhere.

Share this post


Link to post
Share on other sites
I'm somewhat new to mysql, what do you mean by locked?

Share this post


Link to post
Share on other sites
There can be a concurrency issue with non-transaction safe statements.

Share this post


Link to post
Share on other sites
Ah, that makes sense :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.