【问题标题】:How can i convert this PDO statement to active record query in CodeIgniter?如何将此 PDO 语句转换为 CodeIgniter 中的活动记录查询?
【发布时间】:2016-10-20 07:25:20
【问题描述】:

我有这个 PDO 语句可以正常工作,但是如何将它转换为 CodeIgniter 中的活动记录?

我的 PDO 声明是:

UPDATE mytable set total=bought+re_order-balance where retail_id=? and stock=? and stock_date=?

我试过了,但没用:

$data=array(                                                                                            
                    //some other fields
                    'total'=>'bought' + 're_order' - 'balance'                      
                    );

                $this->db->where('retail_id', $someid);
                $this->db->where('stock', $somestock);
                $this->db->where('stock_date', $somestock);
                $this->db->update('mytable',$data); 

当我尝试这个时,它没有同时给出任何错误,它没有更新我在数据库中的总字段。任何帮助请。谢谢。

【问题讨论】:

  • 是否已购买、重新排序和平衡字符串或数字?
  • 它们都是整数。它们也是同一张表中的所有字段。
  • 所以 `$data = array( 'total' => ( $bought + $re-order - $balance ) ) 所以更适合。目前您正在尝试对字符串进行数学运算
  • 另外,我更愿意确保使用表的唯一 ID 进行更新,以上任何 where 语句是否使用表的唯一 ID?
  • 除添加外,其他一切正常。我认为该解决方案行不通,它是一个数据库字段,放置一个“$”使它看起来是一个变量。

标签: php codeigniter activerecord


【解决方案1】:

以下代码应构建所需的查询。 set() 方法的第三个参数防止数据逃逸。你可以在docs阅读更多关于它的信息

$this->db->set('total', 'bought + re_order - balance', FALSE)
            ->where('retail_id', $someid)
            ->where('stock', $somestock)
            ->where('stock_date', $somestock)
            ->update('mytable');

【讨论】:

    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 2012-10-22
    • 2019-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多