3 Replies - 266 Views - Last Post: 18 April 2013 - 09:44 PM Rate Topic: -----

#1 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

PHP + MySql Compound Operators

Posted 18 April 2013 - 06:25 PM

I was wondering if I can set up a php mysql command like this:
$sql = "UPDATE `DatabaseName`.`TableName` SET `ValueIwantToIncrease` += 1 WHERE `TableName`.`id` = 1 LIMIT 1"


the "ValueIwantToIncrease" is stored as a bigint if that makes any difference.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP + MySql Compound Operators

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: PHP + MySql Compound Operators

Posted 18 April 2013 - 06:30 PM

Pretty sure you can't use that type of increment syntax with UPDATEs, but the rest of the query is fine:
$sql = "UPDATE `DatabaseName`.`TableName` SET `ValueIwantToIncrease` = `ValueIwantToIncrease` + 1 WHERE `TableName`.`id` = 1 LIMIT 1"


Was This Post Helpful? 1
  • +
  • -

#3 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: PHP + MySql Compound Operators

Posted 18 April 2013 - 08:48 PM

View Poste_i_pi, on 18 April 2013 - 06:30 PM, said:

Pretty sure you can't use that type of increment syntax with UPDATEs, but the rest of the query is fine:
$sql = "UPDATE `DatabaseName`.`TableName` SET `ValueIwantToIncrease` = `ValueIwantToIncrease` + 1 WHERE `TableName`.`id` = 1 LIMIT 1"


I'll test that out tomorrow, thanks. And i have been doing it by getting the "ValueIwantToIncrease" in one query and then saying "$setToVal = $resultOfQuery + 1" and then assigning "ValueIwantToIncrease = $setToVal" (+ syntax of course when i actually do it in the code), but i have a feeling this will be a lot simpler; thanks.
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: PHP + MySql Compound Operators

Posted 18 April 2013 - 09:44 PM

Yeah, you're better off doing it in the one query, that way you avoid race conditions on the row update.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1