如何在Codeigniter活动记录格式

时间:2015-09-30 07:35:09

标签: php mysql codeigniter

如何编写以下查询

SELECT * FROM `TABLE_NAME` WHERE 1 
在codeigniter活动记录表单中

我尝试过类似的事情:

$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('1');

但是我收到以下错误:

错误号码:1054

'where子句'中的未知列'1'

SELECT * FROM (`TABLE_NAME`) WHERE `1` IS NULL

我使用相同的方法从数据库中选择值。我只是传递列,条件和表名。我的问题是,当我想从表中获取所有数据时,我将在where子句中给出什么?

以下是我的查询部分:

function get($fields,$table,$where)
{
     $this->db->select($fields);
     $this->db->from($table);
     $this->db->where($where); 
     $q = $this->db->get();
     if($q->num_rows() > 0)
     {
         foreach($q->result() as $row)
         {
              $data[] = $row;
         } 
         return $data; 
      }
 } 

按照我在控制器中调用函数的方式:

$data['details'] = $this->MODEL_NAME->get("*",PREFIX."TABLE_NAME",1);

此处的模型是从自动加载中加载的。

1 个答案:

答案 0 :(得分:1)

更改

$this->db->where($where);

if ($where !== null) {
    $this->db->where($where);
}

function get($fields,$table,$where)

function get($fields,$table,$where=null)

因此,当您致电$where时,您可以选择不提供get