Php 限制命令SQL Server

Php 限制命令SQL Server,php,sql-server,select,limit,Php,Sql Server,Select,Limit,这是我的PHP代码: require_once ('ConfigSQL.php'); $per_page = 10; if(isset($_GET['page'])) { $page = ($_GET['page'] - 1); } else { $page = 0; } $QueryCharacter = mssql_query (" SELECT Character.AccountID, Character.Name, Character.CtlCo

这是我的PHP代码:

require_once ('ConfigSQL.php');

$per_page = 10;
if(isset($_GET['page'])) {
    $page = ($_GET['page'] - 1);
}
else {
    $page = 0;
}
    $QueryCharacter = mssql_query ("
        SELECT Character.AccountID, Character.Name, Character.CtlCode, 
        AccountCharacter.Number, AccountCharacter.ID, 
        memb___id, memb_name, memb__pwd2, mail_addr
        FROM Character, AccountCharacter, MEMB_INFO
        WHERE Character.AccountID=AccountCharacter.ID AND 
        AccountID=memb___id AND AccountCharacter.ID=memb___id 
        LIMIT {$page},{$per_page}
        ");

$rows = mssql_fetch_row($QueryCharacter);
$pages = $rows / $per_page;
$pages = $pages ? ((int)$pages == $pages) : ((int)$pages + 1);
这就是我得到的错误:

警告:mssql_query()[function.mssql query]:消息:第2行:“,”附近的语法不正确。第19行C:\xampp\htdocs\Pages\EditCharacter.php中的(严重性15)

警告:mssql_query()[function.mssql query]:在第19行的C:\xampp\htdocs\Pages\EditCharacter.php中查询失败

警告:mssql_fetch_row():提供的参数不是第21行C:\xampp\htdocs\Pages\EditCharacter.php中的有效MS SQL结果资源

我知道问题在于SQL查询中的“限制”


查询应该是怎样的?

尝试将您的查询更改为:

WITH paging AS (
    SELECT
        ,Character.AccountID
        ,Character.Name
        ,Character.CtlCode
        ,AccountCharacter.Number
        ,AccountCharacter.ID
        ,memb___id
        ,memb_name
        ,memb__pwd2
        ,mail_addr
        ,ROW_NUMBER() OVER (ORDER BY Character.AccountID) AS RowNr
    FROM
        Character, 
        AccountCharacter, 
        MEMB_INFO
    WHERE
        Character.AccountID = AccountCharacter.ID
        AND AccountID=memb___id 
        AND AccountCharacter.ID=memb___id 
)
SELECT TOP ({$per_page}) *
FROM paging
WHERE RowNr > {$page} * {$per_page}
ORDER BY RowNr
请注意,第0页是第一页,第1页是第二页,以此类推

这使用了MSSQL 2005中引入的通用表表达式,对于早期版本,类似的内容可能会起作用(源代码):


SQL Server,不限于SQL Server的哪个版本?在2012年,您可以使用OFFSET…FETCH,在以前的版本中,分页要复杂得多(使用CTE和ROW_NUMBER(),或者在前几页中使用TOP)。SQL Server不支持限制(我很高兴它不支持-它不是标准的)。可能是2000年的重复:很久以前。。我已经用一些你可以尝试的东西更新了我的答案。
DECLARE @Sort int

SET ROWCOUNT {$page} * {$per_page}
SELECT @Sort = AccountID FROM Character ORDER BY AccountID

SET ROWCOUNT {$per_page}    
SELECT
    ,Character.AccountID
    ,Character.Name
    ,Character.CtlCode
    ,AccountCharacter.Number
    ,AccountCharacter.ID
    ,memb___id
    ,memb_name
    ,memb__pwd2
    ,mail_addr
    ,ROW_NUMBER() OVER (ORDER BY Character.AccountID) AS RowNr
FROM
    Character, 
    AccountCharacter, 
    MEMB_INFO
WHERE
    Character.AccountID > @Sort
    AND Character.AccountID = AccountCharacter.ID
    AND AccountID=memb___id 
    AND AccountCharacter.ID=memb___id
ORDER BY
    Character.AccountID