我有这个用 php 编写的 sql 查询:
$query = sprintf("UPDATE bank_info SET
amount_dollar = amount_dollar +'$amount_dollar' ,
amount_euro = amount_euro + '$amount_euro' ,
amount_local = amount_local + '$amount_local'
WHERE bank_id = '$bank_id' ");
此查询工作正常,但我想使用 FluentPDO 转换此查询。 我想使用数组来设置值。 例如:
$table='bank_info'; //table name
$arrVal=array(); //values needs to be SET
$arrVal['amount_dollar = amount_dollar+?']=$amount_dollar;
$arrVal['amount_euro = amount_euro+?']=$amount_euro;
$arrVal['amount_local = amount_local+?']=$amount_local;
$arrWhere=array(); //where condition
$arrWhere['bank_id']=$bank_id;
这是查询:
$query = $this->pdo->update($table)->set($arrVal)->where($arrWhere);
$query->execute();
我认为问题出在 $arrVal 中,无法找到正确的方法来 SET 并将值添加到表中列的当前值。 我多次使用数组从数据库/表中选择和获取值,所以我认为 $arrWhere 不是问题。
最佳答案
好吧,找到答案了,
例如:
这对我有用:
$id = 5;
$field = 'stock';
$amount = 1;
$increment = array($field => new FluentLiteral($field.' + '.$amount));
$fpdo->update('products')->set($increment)->where('id', $id)->execute();
关于php - 将 sql 查询转换为 Fluentpdo 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097628/