查询rownumber并查询限制失败的内容

时间:2012-02-13 04:38:19

标签: php mysql codeigniter

我在模型中有两个函数,除了第二个函数在查询字符串中需要数量限制和偏移量之外,它们都是相同的。我在控制器中调用两个,但只有第一个函数执行我想要的操作,第二个函数返回0

public function get_row_nums()
    {
        $commenttb="commenttb";
        $membertb="membertb";
        $query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.userid=`%s`.userid)",
               $commenttb,
               $commenttb,
               $membertb,
               $commenttb,
               $membertb);
        $query=$this->db->query($query);
        print_r($query->result_array());
        return $query->num_rows();
    }
    public function get_comments($num,$offset)
    {
        $commenttb="commenttb";
        $membertb="membertb";
        $query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.userid=`%s`.userid) LIMIT %d,%d",
               $commenttb,
               $commenttb,
               $membertb,
               $commenttb,
               $membertb,
               $num,
               $offset);

        $query=$this->db->query($query);
        print_r($query->result_array());
        return $query;
    }

这是控制器中的功能

public function get_comments()
    {
        if(!file_exists('application/views/blog/list_comment_view.php'))
        {
            show_404();
        }
        else
        {
            $userid=$this->get_userid();
            $row_nums=$this->blog->get_row_nums();
            $config['base_url']=base_url().'index.php/blog/list_comment_view';
            $config['total_rows']=$row_nums;
            print_r($row_nums);
            $config['per_page']=5;
            $config['num_links']=2;
            $config['full_tag_open']='<p>';
            $config['full_tag_close']='<p>';
            $config['first_link']='First';
            $config['last_link']='Last';
            $this->pagination->initialize($config);
            $data['comments']=$this->blog->get_comments($config['per_page'],$this->uri->segment(3));
            $this->load->library('table');
            $this->table->set_heading('No','Title','Created','Modified','Action');
            $this->load->view('blog/list_comment_view', $data);
        }
    }

我试过一次又一次地检查它但是我不知道我可能在源代码中犯了哪些错误,在第二次调用第二个函数时,视图加载了只打印Array()该模型。我很感激任何帮助。

更新
我在配置文件中设置了base_url以指向http://localhost/CodeIgniter/ 我还在Controller中创建了一个名为Blog的文件夹,我存储了所有内容。 我重置了我的默认路由&#34; / blog / LoginClassName&#34 ;; 一切都很好,直到这一部分。

1 个答案:

答案 0 :(得分:0)

正确的LIMIT语法是

 LIMIT OFFSET,LIMIT

因此,您的查询应该看起来像这样

 SELECT commenttb.* FROM commenttb JOIN membertb ON (commenttb.userid=membertb.userid) LIMIT 0,5

表示将$查询更改为此

$query=sprintf("SELECT `%s`.* FROM `%s` JOIN `%s` ON (`%s`.userid=`%s`.userid) LIMIT %d,%d",
           $commenttb,
           $commenttb,
           $membertb,
           $commenttb,
           $membertb,
           $offset,
           $num);
相关问题