Jump to content

Query Math


russellmac

Recommended Posts

I have an innoDB that gets updated often(several times a second) recently I've noticed that when a row gets updated, sometimes instead of doing the math i have defined outside the query it is doubling the exsiting row. for example

$newnumber = $user->points + $amount;
$result = mysql_query("UPDATE `users` SET `points` = '".$newnumber."' WHERE `id`='".$user->id."'");

 

normally it works fine. Say they had 5000 points and $amount was 6 then it would update to 5006 however sometimes it updates to 10012!

I'm thinking that it is possible that two queries maybe running into each other or something and getting added at the same time??

Would it be better to do the math inside the query like this?

$result = mysql_query("UPDATE `users` SET `points`=`points`+'".$amount."' WHERE `id`='".$user->id."'");

Link to comment
https://forums.phpfreaks.com/topic/158522-query-math/
Share on other sites

I don't think something basic like that can be found by searching, you just set a session variable once your code has processed the request and then test at the start of your code is that session variable is already set and skip the processing if it is.

 

<?php
session_start();
// determine if the form processing code has been executed once
$processed = isset($_SESSION['processed']) ? TRUE : FALSE;

if(!$processed) {
    // do your normal form processing here

    // at the point that the form data has been completely and correctly processed, set the session variable to indicate not to process it again during the current session -
    $_SESSION['processed'] = TRUE;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/158522-query-math/#findComment-836073
Share on other sites

OK i will give that a try thanks. Also had a follow up question about Ken2k7 suggestion to remove the sing quotes... would it even be better to just remove all quotes from the variable like this?

$result = mysql_query("UPDATE `users` SET `points`=`points`+$amount WHERE `id`= ".$user->id);

I tried it and it still processed ok, I was just wondering what the benefit of it is.

Link to comment
https://forums.phpfreaks.com/topic/158522-query-math/#findComment-836080
Share on other sites

Archived

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

×
×
  • Create New...

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.