Codeigniter:当表内部有值“”时如何编写查询

I've got a table in which a field contains pattern Like this [{"vendor":"10","status":"paid"}] :

table

I want to make a query 'like' in codeigniter , but I got an error:

model :

function get_total_order($id_vendor){
  $this->db->like('payment_status', 'vendor":"'.$id_vendor.'","status":"due');
  $this->db->from('sale');
  return $this->db->count_all_results();
}

view :

 <?php 
   $new_order = $this->crud_model->get_total_order($this->session->userdata('vendor_id'));
   echo "<h1>".$new_order."</h1>";
 ?>

when i run this, i got blank page, how i fix this? thanks.

You can use $this->db->where_in() like below:-

 $names = array('Frank', 'Todd', 'James');
 $this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')

For more details, please check below link:-

https://www.codeigniter.com/userguide2/database/active_record.html

Since you use "Like" query type, you should add '%' in the query argument or send a complete argument:

function get_total_order($id_vendor)
{
  $this->db->like('payment_status', '%vendor":"'.$id_vendor.'","status":"due%');
  $this->db->from('sale');
  return $this->db->count_all_results();
}

Try this:

function get_total_order($id_vendor){
  $this->db->like('vendor',$id_vendor);
  $this->db->like('status',"due");
  $this->db->from('sale');
  return $this->db->count_all_results();
}

if your searching json data so you have pass the data in like query and like query data should be look like data inside the table how it looks .

your query should be something like this

         <?php


    $id_vendor =123;

    $ss= '%"vendor":"'.$id_vendor.'","status":"due"%';

    $sssss ="select * from sale where  payment_status like '$ss' ";

    echo $sssss;

    query look like this 

        select * from sale where payment_status like '%"vendor":"123","status":"due"%'



    ?>

and also you can use wildcard (%) more place with your wish.

You can customize where as per your requirement with and condition or another condition.

$where = "payment_status like '%$id_vendor%' OR status like '%$status%'"; $this->db->where($where);

try this one:

Because 'like is time consuming.


    function get_total_order($id,$vendor)
    {
      $this->db->where('vender', $id);
      $this->db->where('status',$vendor);
      $this->db->get('sale');
      $result=$res->result_array();
      return $result;
   }