在php中添加LIMIT查询

时间:2017-01-03 02:55:16

标签: php mysql

如何在其中添加LIMIT查询?我在

中添加了它
$SQL = "SELECT * FROM eins_policy LIMIT 500 ";

但它没有显示列表。

enter image description here

例如:

  • 如果我每页选择500条记录;
  • 总记录为3560条记录;
  • 如果现在我在第1页,意思是(页码 - 1)* 500 = 0,我的偏移量是0;
  • 如果现在我在第2页,意思是(页码 - 1)* 500 =(2 - 1)* 500 = 500,而Offset是500,意味着501到1000是提取的记录;
  • 如果现在我在第3页,意思是(页码 - 1)* 500 =(3 - 1)* 500 = 1000,而Offset是1000,意味着1001到1500是提取的记录;

policylist.php

function eins_listPolicy($page, $record ,$search)
{
    //connect to database
    base_connectDatabase();

    $searchs = explode(" ", $search);

    $SQL = "SELECT * FROM eins_policy ";
    $countSearch = 0;

    foreach ($searchs as &$value) {
        if ($countSearch == 0) {
            $SQL .= "WHERE ";
        } else {
            $SQL .= "AND ";
        }
        $SQL .=  "(eins_plcy_code LIKE '%".$value."%' OR
                    eins_plcy_client IN (SELECT eins_clnt_id FROM eins_client WHERE eins_clnt_full_name LIKE '%".$value."%') OR
                    eins_plcy_insurance_company IN (SELECT eins_icomp_id FROM eins_insurance_company WHERE eins_icomp_name LIKE '%".$value."%') OR
                    eins_plcy_agent IN (SELECT eins_agent_id FROM eins_agent WHERE eins_agent_user_id IN (SELECT base_u_id FROM base_users WHERE base_u_name LIKE '%".$value."%')) OR
                    eins_plcy_policy_type IN (SELECT base_u_id FROM base_users WHERE base_u_name LIKE '%".$value."%') OR
                    eins_plcy_start_date LIKE '%".$value."%' OR
                    eins_plcy_end_date LIKE '%".$value."%' OR
                    eins_plcy_payment_status = '".$value."' OR
                    eins_plcy_status LIKE '%".$value."%')
               ";
        $countSearch ++;
    }

    $result = base_executeSQL($SQL);
    $total = base_num_rows($result);
    if(is_int($total/$record)) {
        $tpages = $total/$record;
    } else {
        $tpages =  floor($total/$record)+1;
    }

    $start = $record * ($page-1);
    $adjacents = 3;
    $reload = 'policy_list.php';
    $count = 0;
    $zeroRecord = false;

    $RSQL = "LIMIT ".$start.", ".$record."";

    $getPolicySQL = base_executeSQL($SQL.$RSQL);
    while($Policy_row = base_fetch_array($getPolicySQL))
        if (base_num_rows($getPolicySQL)!= 0) {
            $inscompName = eins_getInscompDetail($Policy_row["eins_plcy_insurance_company"]);
            $agentName = eins_getAgentDetail($Policy_row["eins_plcy_agent"]);
            $clientName = eins_getClientDetail($Policy_row["eins_plcy_client"]);
            $polocytypeName = eins_getPolicytypeDetail($Policy_row["eins_plcy_policy_type"]);
            $count = $count + 1;
            echo "<tr>";
                echo "<td>". $count ."</td>";
                echo "<td><a href = \"../einsurance/view_policy.php?id=". $Policy_row["eins_plcy_id"] ."&page=".$page."&record=".$record."&search=".$search."\">". $Policy_row["eins_plcy_code"] ."</a></td>";
                echo "<td>". $polocytypeName['eins_ptype_name'] ."</td>";
                echo "<td>". $inscompName['eins_icomp_name'] ."</td>";
                echo "<td>". $agentName['eins_agent_full_name'] ."</td>";
                echo "<td>". $clientName['eins_clnt_full_name'] ."</td>";
                echo "<td>". $Policy_row["eins_plcy_payment_status"] ."</td>";
                echo "<td>". $Policy_row["eins_plcy_status"] ."</td>";
                echo "<td><a href = \"../einsurance/edit_policy.php?id=". $Policy_row["eins_plcy_id"] ."&page=".$page."&record=".$record."&search=".$search."\"><img src=\"". BASE_IMG_ICON_EDIT ."\" /></a></td>";
                echo "<td><a href = \"../einsurance/delete_policy.php?id=". $Policy_row["eins_plcy_id"] ."&page=".$page."&record=".$record."&search=".$search."\" onclick=\"return confirm ('".MSG_CONFIRM_DELETE."')\"><img src=\"". BASE_IMG_ICON_DELETE ."\" /></a></td>";
            echo "</tr>";
        }
        if (base_num_rows($getPolicySQL)== 0) {
            echo "<tr><td colspan = \"9\" align = \"center\">There is no record!</td></tr>";
            $zeroRecord = true;
        }
        if($zeroRecord == false) {
            echo "<tfoot bgcolor=\"white\">";
                echo "<tr>";
                    echo "<td colspan=\"10\">";
                        $record .= "&search=".$search;
                        eins_paginate($reload, $page, $tpages, $adjacents, $record, $total) ;
                    echo "</td>";
                echo "</tr>";
        }
        //close the database
        base_closeDatabase();
}

1 个答案:

答案 0 :(得分:0)

LIMIT的正确用法是LIMIT offset, max rows

前500行:... LIMIT 0,500

下一个500:...... LIMIT 500,500等等。

参考:http://dev.mysql.com/doc/refman/5.7/en/select.html