MYSQL限制returns意外结果

MYSQL limit returns unexpected results

<?php
$quuuu = mysql_query("SELECT * FROM products limit 9,15 ") or die("Query Error");
while($ffff=mysql_fetch_array($quuuu)){
    echo "<li><a href='view.php?id=" . $ffff['id'] . "'>" . $ffff['title'] . "</a></li>";
}
echo mysql_num_rows($quuuu);
?>

它应该 return (7), 结果是 (15)

查看文档,https://dev.mysql.com/doc/refman/5.0/en/select.html

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.

根据您的查询,

SELECT * FROM products limit 9,15

您正在告诉 mysql 到 return 从第十行开始的十五行。你从 echo mysql_num_rows($quuuu); 得到 15 因为有 15 行。为什么期望输出7

另请注意,mysql_ 驱动程序已过时,您应该切换到 PDOmysqli。他们有更多有用的功能。

http://php.net/manual/en/migration55.deprecated.php

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.

http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/ref.pdo-mysql.php

您的 LIMIT 使用错误。

Syntax: limit start_at, amount_of_records

如果你想要从第 15 个元素开始的 7 个元素,你应该使用

SELECT * from products limit 14, 7