Codeigniter在db查询中的$ uri和$ perPage

时间:2012-02-21 06:28:08

标签: codeigniter codeigniter-2

我的模型中有以下函数从数据库中获取一些数据,我使用的是codeigniter的默认分页类,为此我在下面的代码中有$ perPage和$ uri。

function payment_due($month,$year,$fee_type,$perPage,$uri ) { 

$getData = $this->db->query("SELECT DISTINCT studentid,studentname FROM
 student  WHERE NOT EXISTS
 (SELECT * FROM studentpayment1 JOIN studentpayment2 
                      ON studentpayment1.paymentid=studentpayment2.pid
WHERE fee_month='$month' AND fee_type='$fee_type' 
 AND fee_year='$year' AND student.studentid=studentpayment1.studentid )
 " );


        if($getData->num_rows() > 0)
        return $getData->result_array();
        else
        return null;
        }

通常当我从表中获取数据时,我使用$ perPage和$ uri,就像这样 - >

$getData = $this->db->get('', $perPage, $uri);

现在我的问题是,请你告诉我在上面的函数中我应该在哪里使用$ perPage和$ uri?

提前致谢:)

如果我在db查询结束时使用$ perPage,$ uri,我会收到以下错误:

  A Database Error Occurred
 Error Number: 1064

You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL  server version for the right syntax to
use near '15' at line 3

 SELECT DISTINCT studentid,studentname FROM student WHERE NOT EXISTS
   (SELECT * FROM   studentpayment1 JOIN studentpayment2
  ON  studentpayment1.paymentid=studentpayment2.pid WHERE 
 fee_month='February' AND fee_type='Monthly Fee' AND fee_year='2012'
  AND student.studentid=studentpayment1.studentid ) LIMIT ,15

  Filename: C:\xampp\htdocs\utc\system\database\DB_driver.php

行号:330 `

1 个答案:

答案 0 :(得分:0)

user_guide中,您可以看到$this->db->get()的代码示例以及生成的内容:

  

第二个和第三个参数使您可以设置限制和偏移   子句:

$query = $this->db->get('mytable', 10, 20);

// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)

所以基本上你需要在你的查询中设置LIMIT,在它的末尾添加类似的东西:

LIMIT $uri,$perPage