mysql LIMIT关键字的分页不起作用

时间:2012-08-15 10:46:24

标签: php mysql limit paginate

我试图在检查页数后,在SELECT语句中使用LIMIT关键字对30个RECORDS分页结果,我使用了2个循环,FOR循环用于标题,WHILE循环用于结果行,并限制使用LIMIT, 需要知道我是否可以在LIMIT关键字后使用变量。我的代码如下:

<?php 
    include 'dbconnect.php'; 

    ?>

    //--CHECKING number of records from STOCK table------------------------------------
    $di=mysql_query("SELECT * FROM CYLSTOCK_draft WHERE (DC_Date BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') OR (Ecr_dt BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') ORDER BY DC_Date");

    $numrows=mysql_num_rows($di);

    // number of rows to show per page
    $rowsperpage = 30;
    // find out total pages
    $totalpages = ceil($numrows / $rowsperpage);

    if ($numrows>0)
    {
        $sn=0;
        //--------for loop for Paging-------------
        for ($x=0; $x < $totalpages; $x++)
        {
            echo "<br><table width='622' border='1' cellspacing='0' cellpadding='5' align='center'>";
            echo "<tr>";
            echo "<td width='30' align='center' class='toprow'>SNo.</td>";
            echo "<td width='86' align='center' class='toprow'>DC Date</td>";
            echo "<td width='94' align='center' class='toprow'>DC No.</td>";
            echo "<td width='160' align='center' class='toprow'>Product Description</td>";
            echo "<td width='89' align='center' class='toprow'>Cylinder No.</td>";
            echo "<td width='91' align='center' class='toprow'>ECR Date</td>";
            echo "<td width='72' align='center' class='toprow'>ECR No.</td>";
            echo "</tr>";

            //---while loop with select statement limit to 30 records---------------------------------
            $dis=mysql_query("SELECT * FROM CYLSTOCK_draft WHERE (DC_Date BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') OR (Ecr_dt BETWEEN '$startdat' AND '$enddat' AND Ac_code='$custno' AND Product_Desc='$gas') ORDER BY DC_Date LIMIT '$sn', 30");
            while ($di2=mysql_fetch_assoc($dis))
            {
                $sn++;
                $k3=$di2['DC_Date'];
                $date_array = explode( "-", $k3 );
                $kk3 = sprintf( '%02d-%02d-%4d', $date_array[2], $date_array[1], $date_array[0]);
                $kk3a=strtotime($kk3);

                $k2=$di2['DC_No'];
                $k6=$di2['Product_Desc'];
                $k7=$di2['Cylinder_No'];
                $k5=$di2['Ecr_dt'];
                $kk5=strtotime($k5);
                $k4=$di2['Ecr_No'];

                echo "<tr>";
                echo "<td width='30' align='center' class='sty1'>$sn</td>";
                echo "<td width='86' align='center' class='sty1'>";
                if ($kk3a>0)
                {
                    echo $kk3;
                }
                echo "</td>";

                echo "<td width='94' align='center' class='sty1'>$k2</td>";
                echo "<td width='160' align='center' class='sty1'>$k6</td>";
                echo "<td width='89' align='center' class='sty1'>$k7</td>";
                echo "<td width='91' align='center' class='sty1'>";
                if($kk5>0)
                {
                    $date_array = explode( "-", $k5 );
                    $kk5 = sprintf( '%02d-%02d-%4d', $date_array[2], $date_array[1], $date_array[0]);
                    echo $kk5;
                }
                echo "</td>";

                echo "<td width='72' align='center' class='sty1'>";
                if (substr($k4,5) > 0)
                {
                    echo $k4;
                }
                echo "</td>";
                echo "</tr>";
            }
        //---End of for loop
        }
    }
    echo "</table><br>";

1 个答案:

答案 0 :(得分:2)

.. LIMIT '$sn', 30");  ..

$ sn

周围没有引号