为什么CodeIgniter会在此查询中添加额外的星号?

时间:2012-08-10 18:17:09

标签: php codeigniter

我有以下代码,我无法弄清楚CodeIgniter为其生成的查询添加额外的通配符的原因。

代码

class Foo_Model extends CI_Model {
  private $table = 'foo';

  public function get_all_foos() {
    $table = $this->table;   
    $this->db->select("$table.*")->from($table);
    return $this->get()->result();
  }
}

我收到以下错误:

  

发生数据库错误

     

错误号码:1064

     

您的SQL语法有错误;查看与您的MySQL>服务器版本对应的手册,以便在第1行的'* FROM(foofoo)附近使用正确的语法

     

SELECT foo。*,* FROM(foofoo

为什么查询生成错误?

3 个答案:

答案 0 :(得分:2)

如果要选择所有内容,请不要使用->select()语句。这应该等同于您的预期。

class Foo_Model extends CI_Model {
  private $table = 'foo';

  public function get_all_foos() {
    $table = $this->table;   
    $this->db->from($table);
    return $this->get()->result();
  }
}

请参阅文档中的$this->db->get以获取示例 - http://codeigniter.com/user_guide/database/active_record.html

答案 1 :(得分:0)

我认为这是Codeigniter 2.1.2的错误

这应该适合你:

$this->db->select("`$table`.*",false)->from($table);

答案 2 :(得分:0)

如果您想选择所有内容,最快捷的方法是直接使用get()表名。
 例如:

class Foo_Model extends CI_Model {
  private $table = 'foo';

  public function get_all_foos() {
   $table = $this->table;
   return $this->db->get($table)->result();
  }
}
相关问题