php - 将 sql 查询转换为 Fluentpdo 查询

标签 php mysql fluentpdo

我有这个用 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/

相关文章:

php - WordPress:如何获取标题图像 ID

php - 单击链接运行 sql

javascript - 如何在 php 循环创建的表单中分配变量或使用数字?

php - 如何在 1 个 SQL 查询中的 2 个条件下获得 2 个总和?

c# - 存储过程产生从 c# .net 调用的不同结果

php - codeigniter 中的 SQL LIKE 运算符

php - 未定义的类常量 'PDO::FETCH_DEFAULT'

php - 如何阻止 FluentPDO 错误地推断表名

php - 如何以多行形式保存用户名和user_id

php - FluentPDO 中的子查询