php - 在 Get_Where 查询中匹配 JSON 字段(Code Igniter)

标签 php mysql json codeigniter

我正在 get_where 查询中执行 result_array(),但“added_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()

最佳答案

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 . ''))

关于php - 在 Get_Where 查询中匹配 JSON 字段(Code Igniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44682635/

相关文章:

php - 将成员添加到 MailChimp 列表时出现 400 错误请求

php - Laravel JSON 从 POST 读取

android - Kotlin 数据类 |添加我自己的变量而不是 JSON 键

jquery - 使用 JSON 数据填充下拉菜单

Java GDAX 身份验证的 REST 请求 HTTP GET 错误 400

php - 从mysql数据库中删除坏字符

java - 如何为 Web 应用程序创建文件服务器

php - MySQL 查询问题(重复结果)

mysql - 如何查找与全部或部分输入完全匹配的字符串

php - 将缩略图插入数据库