【问题标题】:PHP Codeigniter Database Helper Class "No tables used"PHP Codeigniter 数据库助手类“未使用表”
【发布时间】:2012-10-06 20:05:56
【问题描述】:

好的,这显然不是完整的代码,但这是错误所在的代码。

$this->db->select('post_likes');
$this->db->from('user_dash_posts');
$this->db->where('post_idn', $idn);
$this->db->where('isactive', '0');
if($this->db->count_all_results() > 0)
{
    $dataDB = $this->db->get();

关于我在此处显示的最后一行的 get() 语句,我得到“未使用表”。我对整个 CI 数据库类/帮助器还是有点陌生​​,因为我以前更喜欢手写完整的查询器。但我正在尝试在 ORM 层上自学并更好地熟悉它们。然而,这对我的问题来说是一个无声的点。我试图弄清楚我在这句话中做错了什么,解决方法是什么,如果可能的话,解释一下我是如何做错的,这样我就可以理解而不是再犯了。

【问题讨论】:

    标签: php mysql database codeigniter orm


    【解决方案1】:

    根据documentation

    通常,当一个 Active Record 调用完成时,所有存储的信息都会为下一次调用重置。

    如果您想重用查询,请使用可以在同一页面底部找到的缓存方法。完成后不要忘记刷新缓存:

    $this->db->start_cache();
    $this->db->select('post_likes');
    $this->db->from('user_dash_posts');
    $this->db->where('post_idn', $idn);
    $this->db->where('isactive', '0');
    $this->db->stop_cache();
    
    if($this->db->count_all_results() > 0) {
         $dataDB = $this->db->get();
    }
    $this->db->flush_cache();
    

    编辑:

    使用单个查询而不是两个查询的另一种选择:

    $this->db->select('post_likes');
    $this->db->from('user_dash_posts');
    $this->db->where('post_idn', $idn);
    $this->db->where('isactive', '0');
    $query = $this->db->get();
    
    if($query->num_rows() > 0) {
         $dataDB = $query->result();
    }
    

    【讨论】:

      【解决方案2】:

      您可以简单地删除$this->db->from('user_dash_posts'); 行并将您的表名添加到get() 函数中,如下所示:$dataDB = $this->db->get('user_dash_posts');

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 1970-01-01
        相关资源
        最近更新 更多