0

这是我的 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 行:',' 附近的语法不正确。(严重性 15)在第 19 行的 C:\xampp\htdocs\Pages\EditCharacter.php

警告: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 查询中的“限制”。

查询应该如何?

4

1 回答 1

1

尝试将您的查询更改为:

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 中引入的公共表表达式,对于早期版本,这样的东西应该可以工作(源Efficient Paging (Limit) Query in SQLServer 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
于 2013-08-26T21:10:04.353 回答