PHP分页查询无法正常工作

时间:2013-09-27 05:47:26

标签: php mysql

在我的控制器中,我有以下代码:

if($action=='shipping_list'){ 
    $numrows=inquire_row(); //Get number of rows in query
    $pages= new Paginator('10', 'p'); //create object
    $pages->set_total($numrows);
    $shipping=shipping_list(); //Goes to model
    include('shipping_list.php');
}

在我的模型中我是这些代码:

function shipping_list(){
    global $MEMS;
    global $pages;
        $query = "SELECT * FROM Inventory" .$pages->get_limit()
        ."WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' '
        ORDER BY Wafer ASC, RC ASC";
        echo $query;
    $shipping = $MEMS -> query($query);
    var_dump($shipping);
    return $shipping;
}

当我回显$ query时,我得到了

SELECT * FROM Inventory LIMIT 0, 20 WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' ' ORDER BY Wafer ASC, RC ASC

所以我知道一切都是正确的。但是,当我var_dump $ shipping时,我得到了bool(false)。为什么我的查询没有返回正确的结果?

3 个答案:

答案 0 :(得分:1)

您的查询正在错误的位置添加LIMIT标记。

LIMIT标签应该在最后。

所以,你的查询应该是:

SELECT * FROM Inventory WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' ' ORDER BY Wafer ASC, RC ASC LIMIT 0, 20

将模型中的SQL修改为:

function shipping_list(){
    global $MEMS;
    global $pages;
        $query = "SELECT * FROM Inventory 
        WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' '
        ORDER BY Wafer ASC, RC ASC " .$pages->get_limit(); // ADD LIMIT tag at the end.
        echo $query;
    $shipping = $MEMS -> query($query);
    var_dump($shipping);
    return $shipping;
}

答案 1 :(得分:0)

LIMIT应该在查询的末尾

 $query = "SELECT * FROM Inventory WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' ' ORDER BY Wafer ASC, RC ASC ".$pages->get_limit();

答案 2 :(得分:0)

分页查询看起来像

$sql= "SELECT * FROM $tableName LIMIT $start, $limit";

您必须指定$start$limit

我们假设第一页有20个结果。

$sql= "SELECT * FROM $tableName LIMIT 0, 20";

在你的情况下,它会改变为类似的东西

$query = "SELECT * FROM Inventory WHERE Yield >=320 AND (Q = 'Pass' OR Q='No Q') AND shipdate = ' ' ORDER BY Wafer ASC, RC ASC ".$pages->get_limit();