简体   繁体   English

如何在Codeigniter Active记录表单中使用WHERE 1编写mysql查询

[英]How to write mysql query with WHERE 1 in Codeigniter Active record form

How to write the following query 如何编写以下查询

SELECT * FROM `TABLE_NAME` WHERE 1 

in codeigniter Active record form ? 以codeigniter活动记录形式?

I tried something like: 我尝试了类似的东西:

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

But I am getting the following error: 但是我收到以下错误:

Error Number: 1054 错误编号:1054

Unknown column '1' in 'where clause' “ where子句”中的未知列“ 1”

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

I am using same method for selecting values from database. 我正在使用相同的方法从数据库中选择值。 I just pass the columns, condition and table name. 我只传递列,条件和表名。 My problem is, when I want to get all the data from a table, what will I give in the where clause ? 我的问题是,当我想从表中获取所有数据时,在where子句中会给出什么?

Following is my query section: 以下是我的查询部分:

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; 
      }
 } 

Following how I call the function in controller: 以下是我在控制器中调用函数的方式:

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

The model here is loaded from autoload. 这里的模型是从自动加载中加载的。

Change 更改

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

into

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

and

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

into

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

so you can opt not to give $where when you call get . 因此您可以在调用get时选择不给$where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM