【发布时间】:2013-04-26 22:48:11
【问题描述】:
我正在使用我的 sql 2008 db 使用 CI 分页助手。
我在模型上的功能是:
function get_data($limit, $offset) {
$billing_db = $this -> load -> database('billing', TRUE);
$billing_db -> select('stuff, stuff2, stuff3');
$billing_db -> from('mytable');
$billing_db -> limit($limit, $offset);
$this -> db -> order_by("id", "asc");
$q = $billing_db -> get();
return $q;
}
现在在我的控制器上,我调用了如下函数:
$data['billers'] = $this -> billing_model -> get_data(10, $this -> uri -> segment(3));
当我默认打开页面时,它会正确显示 10 个条目。
然后当我更改页面时问题就开始了,假设我点击下一步。
现在 url 段 3 是 10。应该从第 10 个条目开始并限制为 10。
但发生的事情是它从条目 1 开始并显示 20 条记录。
每次偏移量变高时,它只会从头开始显示更多记录。
可能出了什么问题?
/**
* Limit string
*
* Generates a platform-specific LIMIT clause
*
* @access public
* @param string the sql query string
* @param integer the number of rows to limit the query to
* @param integer the offset value
* @return string
*/
function _limit($sql, $limit, $offset)
{
$i = $limit + $offset;
return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
}
【问题讨论】:
-
我想您获得越来越多记录的原因是 _limit() 正在添加要在查询中使用的偏移量和限制。因此,当您浏览它们时,您的页面正在查询 SELECT TOP 10、SELECT TOP 20、SELECT TOP 30 等。我没有足够的 SQL Server 2008 经验,不知道如何正确地进行偏移。
标签: php sql codeigniter limit