【问题标题】:CodeIgniter where and like sql query statementCodeIgniter where和like sql查询语句
【发布时间】:2014-05-26 11:34:31
【问题描述】:

我正在尝试在 codeigniter 上执行此 sql 查询

SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'

我不确定它是否正确......

$this->db->get_where('person' , array('type'=>'staff'))
         ->like('description','university%')
         ->result_array();

有人知道我的案子吗?在此先感谢...

【问题讨论】:

  • 对于查询调试,请考虑使用 CI 分析器,您将始终知道您正在运行什么查询$this->output->enable_profiler(TRUE);

标签: php mysql sql codeigniter


【解决方案1】:

使用Active Record 你可以这样做

$query = $this->db->select('*')
             ->from('person')
             ->where('type','staff')
             ->like('description','university','after')
             ->get();

$result = $query->result_array();

确保您将after 作为like() 函数中的第三个参数传递,以便活动记录将在university 之后添加通配符,即%,因此它看起来像LIKE 'university%'

【讨论】:

    【解决方案2】:

    docs 对此进行了很好的解释,但他们的 API 非常简单。要生成查询,您需要如下所示的相关代码:

    $this->db->select('*')
         ->from('person');
         ->where('type', 'staff')
         ->like('description','university');
    
    $query  = $this->db->get();
    $result = $query->result_array(); 
    

    【讨论】:

      【解决方案3】:

      我从未使用过链接(尽管我知道这是可能的),但分解你的问题应该很容易;

      $this->db->from('person');
      $this->db->where('type','staff');
      $this->db->where('description', 'university%');
      $query = $this->db->get();
      $result = $query->result_array();
      return $result;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-18
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 2016-01-21
        • 2017-09-12
        • 1970-01-01
        • 2021-10-30
        相关资源
        最近更新 更多