gz123 发表于 2008-10-22 15:25:24

江湖救急,关于$this->db->limit()

在数据库查询的一个models里面,代码如下
function getInfoByArr($wherearr,$orderstr="",$pagenum){
       $this->load->database();
       $this->db->select('id,bookname, author, isbn, press, out_date, price, sort_name');
       //设置查询条件
       if ($wherearr['bookname'] != '')
         $this->db->where('bookname',$wherearr['bookname']);
       if ($wherearr['isbn'] != '')
         $this->db->where('isbn',$wherearr['isbn']);
       if ($wherearr['sort_id'] != "")
         $this->db->where_in('sort_id',split(",",$wherearr['sort_id']));
       if ($wherearr['author'] != '')
         $this->db->where('author',$wherearr['author']);
       if ($wherearr['price1'] != 0 and $wherearr['price2'] != 0){
         $this->db->where('price >',$wherearr['price1']);
         $this->db->where('price <',$wherearr['price2']);
       }
                  
         
       //设置排序条件
       if ($orderstr != "")
         $this->db->order_by($orderstr,"desc");
      
   $this->db-limit($pagenum,20) ;
       $query = $this->db->get('t_book');
       if ($query->num_rows()>0) {
      $result = $query->result_array();
      return $result;
       }else {
      return "";
       }
      }

让人很奇怪的是这句:
$this->db-limit($pagenum,20) ;    //只要加上这个设置limit就查不出数据,只要把这句去掉就正常,为什么很郁闷
去掉这句,改写成$query = $this->db->get('t_book',$pagenum,20);也不行,还是查不出数据,为啥呢

[ 本帖最后由 gz123 于 2008-10-22 15:29 编辑 ]

gz123 发表于 2008-10-22 15:33:05

知道为什么了,是写反了了,应该是$this->db->limit(20,$pagenum);

LEMON 发表于 2008-10-22 17:22:02

呵呵,我也吃过一次亏,大家好好看看手册吧。
http://codeigniter.org.cn/user_guide/database/active_record.html

$this->db->limit();
Lets you limit the number of rows you would like returned by the query:
$this->db->limit(10);
// Produces: LIMIT 10

The second parameter lets you set a result offset.

$this->db->limit(10, 20);
// Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)

Terence 发表于 2008-10-22 21:36:07

从第20条记录开始查询,你数据库有20条记录?没有,当然不显示

xjflyttp 发表于 2008-10-23 22:44:34

手册写得很清楚...跟mysql的limit反过来....

gz123 发表于 2008-10-23 22:46:31

不是从20条开始查询,是因为我们以前用SQL习惯了 limit0,20(从0条开始,查询出20条) ,而在CI里面这个
$this->db->limit('xxx','xxx')是反的,如果要实现上面的从0条开始查询20条,不是$this->db->limit(0,20)而是
$this->db->limit(20,0)就是习惯思维造成的,看手册的时候这部分估计大家都是一略而过了,我就是吃了这个亏,折腾了一个小时
页: [1]
查看完整版本: 江湖救急,关于$this->db->limit()