【问题标题】:Match A JSON Field In Get_Where Query (Code Igniter)匹配 Get_Where 查询中的 JSON 字段 (Codeigniter)
【发布时间】:2017-11-24 17:50:46
【问题描述】:

我在 get_where 查询中执行了 result_array(),但 'add_by' 中的列是 json 格式到 {'id': '(the id)'}

如何执行 get_where 查询以匹配当前用户 ID。

这是我目前所拥有的。

$this->db
     ->order_by('till', 'desc')
     ->get_where('coupon', 
          [
               'added_by' => json_encode(array('id'=> $this->session->user_id)), 
               'status' => 'ok'
          ]
     )
     ->result_array()

【问题讨论】:

    标签: php mysql json codeigniter


    【解决方案1】:

    json_encode 将返回 {"id": $id}。如果你对{"id": "$id"} 没问题,那么你只需要强制强制字符串:

    json_encode(array('id'=> $this->session->user_id . ''))
    

    如果您需要{'id': '$id'},则需要手动进行字符串替换。

    str_replace( '"', '\'', json_encode(array('id'=> $this->session->user_id . ''))
    

    【讨论】:

    • 这只有在数据库中它的 {'type': and 'id'} all single ' 才有效...我需要它使用像 {"type": "id"
    • @Gilbert 如果需要加倍,那就用第一个例子:-)
    猜你喜欢
    • 1970-01-01
    • 2017-06-12
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多