0

I am trying to update some records which uses a function, and the function's parameter accept's the columns current value.

For example, when doing an increment update query, I can do:

$query = $pdo->prepare("UPDATE tbl SET col1 = col1 + 1 WHERE id = someId");

Surely that works,

But how about some complicated computations, for example:

$query = $pdo->prepare("UPDATE tbl SET col1 = {someFunc(`col1`)} WHERE id = someId");

Now, this is not working, perhaps it passes the col1 as a literal col1 instead of col1's database value.

Thus, the question's title.

jthinam
  • 279
  • 2
  • 7
  • 1
    [You need to concatenate the response into the string](https://stackoverflow.com/questions/27494038/concatenate-php-function-output-to-a-string-like-variables), or better yet, use parameter binding and use [bindValue](http://php.net/manual/en/pdostatement.bindvalue.php) – aynber Sep 06 '22 at 14:15
  • you have to either recreate this function's payload using mysql functions or select the col1 value first (but in this case the action won't be atomic) – Your Common Sense Sep 06 '22 at 14:18
  • Is `someFunc` a PHP function or a mysql function? It's a bit unclear. If it's a PHP function you'd need to SELECT the relevant data first, get it into PHP, run the function, get the output and then UPDATE the relevant row in mysql afterwards. – ADyson Sep 06 '22 at 15:50

0 Answers0