简体   繁体   English

MYSQL限制返回意外结果

[英]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);
?>

its should return (7), and the result is (15) 它应该返回(7),结果是(15)

See the doc, https://dev.mysql.com/doc/refman/5.0/en/select.html . 参见文档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子句可用于约束SELECT语句返回的行数。 LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements). LIMIT接受一个或两个数字参数,这两个参数都必须是非负整数常量(使用预处理语句时除外)。

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. 有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。

So with your query, 因此,根据您的查询,

SELECT * FROM products limit 9,15 SELECT * FROM产品限制9,15

You are telling mysql to return fifteen rows starting at the tenth row. 您要告诉mysql从第十行开始返回十五行。 You are getting 15 from echo mysql_num_rows($quuuu); 您将从echo mysql_num_rows($quuuu);获得15 echo mysql_num_rows($quuuu); because there are 15 rows. 因为有15行。 Why do you expect 7 to be outputted? 为什么期望输出7

Also note that the mysql_ driver is depreciated, you should switch over to PDO or mysqli . 另请注意, mysql_驱动程序已弃用,您应切换到PDOmysqli They have a lot more useful features. 它们具有更多有用的功能。

http://php.net/manual/en/migration55.deprecated.php 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. 现在不推荐使用原始的MySQL扩展,并且在连接数据库时会生成E_DEPRECATED错误。 Instead, use the MySQLi or PDO_MySQL extensions. 而是使用MySQLi或PDO_MySQL扩展。

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

Your LIMIT use is wrong. 您的LIMIT使用有误。

Syntax: limit start_at, amount_of_records

If you want 7 elements starting at 15th element, you should use 如果要从第15个元素开始有7个元素,则应使用

SELECT * from products limit 14, 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM