Mathy Posted June 25, 2007 Share Posted June 25, 2007 $sql="SELECT points, gold, posts FROM users WHERE uname='" . Decrypt_Text($UN) . "'"; $r = mysql_query($sql, $conn); if(mysql_affected_rows()>0) { $l = mysql_fetch_array($r); $sqla="UPDATE users SET points='" . $l["points"] + 2 . "', gold='" . $l["gold"] + 5 . "', posts='" . $l["posts"] + 1 . "' WHERE uname='" . Decrypt_Text($UN) . "'"; echo $sqla; } The code above echoes "8' WHERE uname='Mathy'" What did I do wrong here? Isn't it possible to add numbers to strings? Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/ Share on other sites More sharing options...
kael.shipman Posted June 25, 2007 Share Posted June 25, 2007 What do you mean by add numbers to strings? If you want "this" + 1 to equal "this1", then you have to use the concatenator ".": "this".1 would equal "this1". I would assume by your example, though, that you already know that, so please clarify your question. Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282011 Share on other sites More sharing options...
Mathy Posted June 25, 2007 Author Share Posted June 25, 2007 Yeah well, the $L array all contains numbers, so by $l['posts'] + 2 I mean the content of the $l['posts'] + 2 which would be 4 if $l['posts'] was 2. General calculation. Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282014 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 and what is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282015 Share on other sites More sharing options...
akitchin Posted June 25, 2007 Share Posted June 25, 2007 my guess is that you're wrongly assuming what the $l array contains before running the calculations on it. try running: echo '<pre>'.print_r($l, TRUE); just before the query to see what your values prior to the UPDATE are. Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282016 Share on other sites More sharing options...
Mathy Posted June 25, 2007 Author Share Posted June 25, 2007 Well, "echo $sqla;" echoes "8' WHERE uname='Mathy'". So, if $l['points'], $l['gold'] and $l['posts'] was 1, it should instead echo: "UPDATE users SET points='3', gold='6', posts='2' WHERE uname='Mathy'" Why does it break it all up? Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282017 Share on other sites More sharing options...
Mathy Posted June 25, 2007 Author Share Posted June 25, 2007 my guess is that you're wrongly assuming what the $l array contains before running the calculations on it. try running: echo '<pre>'.print_r($l, TRUE); just before the query to see what your values prior to the UPDATE are. I know it contains something! I am almost sure. However, I should look at it, you are right. I will be replying shortly. Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282019 Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 <?php $sql="SELECT `points`, `gold`, `posts` FROM `users` WHERE `uname`='" . Decrypt_Text($UN) . "'"; $r = mysql_query($sql, $conn); if(mysql_affected_rows()>0) { $l = mysql_fetch_array($r); $po=$l["points"]+2; $g=$l["gold"]+5; $p=$l["posts"]+1; $po=$_POST['po']; $g=$_POST['g']; $p=$_POST['p']; $sqla="UPDATE `users` SET `points`='$po' AND `gold`='$g' AND `posts`='$p' WHERE `uname`='" . Decrypt_Text($UN) . "'"; $result=mysql_query($sqla); echo $sqla; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282025 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 try $sqla="UPDATE users SET points='" . ($l["points"] + 2) . "', gold='" . ($l["gold"] + 5) . "', posts='" . ($l["posts"] + 1) . "' WHERE uname='" . Decrypt_Text($UN) . "'"; i try to explain PHP convert number to string and string to number, operator + except numbers and operator . except strings old code say "UPDATE users SET points='" . $l["points"] produce something like "UPDATE users SET points='5" type strimg "UPDATE users SET points='5" + 2 convert "UPDATE users SET points='5" to number produce 0 and add 2 result is 2 (number) 2. "', gold='" . $l["gold"] => "2, gold='3" (string) "2gold='3" + 5 (1st "2gold='3" to number => 2) 2 + 5 = 7 etc Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282044 Share on other sites More sharing options...
kael.shipman Posted June 25, 2007 Share Posted June 25, 2007 So it's a problem of parentheses and operator precedence, then. Good call, sasa. In that case you could either use the nice clean solution laid out by redarrow or just put your addition in parentheses and concatenate (like "UPDATE users set points = '".($l['points'] + 2)."', ...."). Quote Link to comment https://forums.phpfreaks.com/topic/57075-my-first-request-calculation-with-strings/#findComment-282049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.