codeigniter活动记录得到的查询和查询没有LIMIT子句 [英] codeigniter active record get query and query without the LIMIT clause

查看:129
本文介绍了codeigniter活动记录得到的查询和查询没有LIMIT子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用的活动记录,它的所有工作确定,但我想将 $数据[totalres] 以总成绩,我的意思是,相同的查询,但没有在 LIMIT

问题是previous报表变得没有设置,当你做一个查询,修改,所以我甚至不能添加$这个 - > DB->限制()后,我得到的结果。

什么想法?我认为它是一种不好的做法,以复制的查询只是为了做到这一点

 函数get_search($开始,$ numRows行,$过滤器=阵列())
{

    ...

    $这个 - > DB
     - >选择(EMP)
     - 肽从('EMP')
     - >加入('EMPR','empr.b = empr.id','左')
     - >像('code',$code)
     - >极限($ numRows行,$开始);

    ...

    $ Q = $这个 - > DB->获得();

    //行没有LIMIT X个,Y型过滤器
    $数据[totalres] = ???????;

    如果($ Q-> NUM_ROWS()大于0)
    {
        $数据[成果] = $ Q->的结果();
    } 其他 {
        $数据[成果] =阵列();
    }

    返回$的数据;
}
 

解决方案

您可以使用<一个href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows"><$c$c>SQL_CALC_FOUND_ROWS得到的行数,将已经返回SANS - 限制。注意,在选择行假。这告诉codeIgniter不要试图逃脱反引号中的 SELECT 子句(因为 SQL_CALC_FOUND_ROWS 不是一个领域,和codeIgniter没有意识到这一点)。

  $这个 - &GT; DB
 - &GT;选择(SQL_CALC_FOUND_ROWS EMP,FALSE)
 - 肽从('EMP')
 - &GT;加入('EMPR','empr.b = empr.id','左')
 - &GT;像('code',$code)
 - &GT;极限($ numRows行,$开始);

$ Q = $这个 - &GT; DB-&GT;获得();
 

然后在那之后查询被运行,我们需要运行另一个查询来获取的行的总数。

  $查询= $这个 - &GT; DB-&GT;查询(SELECT FOUND_ROWS()AS`Count`');
$数据[totalres] = $查询 - &GT;排() - &GT;计数;
 

im using active record, its all working ok but i want to set the $data["totalres"] to the total results, i mean, the same query but without the LIMIT

the problem is the previous statements gets unset when you do a query modifier, so i cant even add the $this->db->limit() after i get the results.

any ideas? i think its a bad practice to 'duplicate' the query just to do this

function get_search($start, $numrows, $filter = array())
{    

    ...

    $this->db
    ->select("emp")
    ->from('emp')
    ->join('empr', 'empr.b = empr.id', 'left')
    ->like('code', $code)
    ->limit($numrows, $start);

    ...

    $q = $this->db->get();        

    // number of rows WITHOUT the LIMIT X,Y filter
    $data["totalres"] = ???????;        

    if ($q->num_rows() > 0)
    {        
        $data["results"] = $q->result();
    } else {
        $data["results"] = array();
    }   

    return $data;
}    

解决方案

You can use SQL_CALC_FOUND_ROWS to get the number of rows that would have been returned sans-LIMIT. Note the ,FALSE in the select line. This tells CodeIgniter not to try to escape the SELECT clause with backticks (because SQL_CALC_FOUND_ROWS is not a field, and CodeIgniter doesn't realize that).

$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);

$q = $this->db->get();

Then after that query is ran, we need run another query to get the total number of rows.

$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $query->row()->Count;

这篇关于codeigniter活动记录得到的查询和查询没有LIMIT子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆