当前位置:Gxlcms > mysql > PHPmysql中limit用法详解(代码示例)

PHPmysql中limit用法详解(代码示例)

时间:2021-07-01 10:21:17 帮助过:53人阅读

在MySQL中,LIMIT子句与SELECT语句一起使用,以限制结果集中的行数。LIMIT子句接受一个或两个offset和count的参数。这两个参数的值都可以是零或正整数。

offset:用于指定要返回的第一行的偏移量。

Count:用于指定要返回的最大行数。

Limit子句接受一个或两个参数,当指定两个参数时,第一个参数是偏移量,第二个参数表示计数,而当只指定一个参数时,它表示从结果集开始返回的行数。

LIMIT语法:

  1. SELECT column1, column2, ...
  2. FROM table_name
  3. LIMIT offset, count;

如下表“Data”,其中包含三列“Firstname”、“Lastname”和“Age”。

b0bbb7fc631039addae8795a14fa7ae.png

要从“Data”表中检索前三行,我们将使用以下查询:

  1. SELECT * FROM Data LIMIT 3;

要从“Data”表中检索第2-3行(包括),我们将使用以下查询:

  1. SELECT * FROM Data LIMIT 1, 2;

下面是PHP mysql实现查询的代码示例:

示例1:Limit条件

  1. <?php
  2. $link = mysqli_connect("localhost", "root", "", "Mydb");
  3. if ($link == = false) {
  4. die("ERROR: Could not connect. ".mysqli_connect_error());
  5. }
  6. $sql = "SELECT * FROM Data LIMIT 2";
  7. if ($res = mysqli_query($link, $sql)) {
  8. if (mysqli_num_rows($res) > 0) {
  9. echo "<table>";
  10. echo "<tr>";
  11. echo "<th>Firstname</th>";
  12. echo "<th>Lastname</th>";
  13. echo "<th>Age</th>";
  14. echo "</tr>";
  15. while ($row = mysqli_fetch_array($res)) {
  16. echo "<tr>";
  17. echo "<td>".$row['Firstname']."</td>";
  18. echo "<td>".$row['Lastname']."</td>";
  19. echo "<td>".$row['Age']."</td>";
  20. echo "</tr>";
  21. }
  22. echo "</table>";
  23. mysqli_free_result($res);
  24. }
  25. else {
  26. echo "No matching records are found.";
  27. }
  28. }
  29. else {
  30. echo "ERROR: Could not able to execute $sql. ".mysqli_error($link);
  31. }
  32. mysqli_close($link);

输出:

b1ad7d3eeb2f25037173ca66807f8a3.png

注:“res”变量存储函数mysql_query()返回的数据。

每次调用mysqli_fetch_array()时,它都会从res()集中返回下一行。

while循环用于遍历表“data”的所有行。

示例2:使用面向对象方法的Limit子句

  1. <?php
  2. $mysqli = new mysqli("localhost", "root", "", "Mydb");
  3. if ($mysqli == = false) {
  4. die("ERROR: Could not connect. ".$mysqli->connect_error);
  5. }
  6. $sql = "SELECT * FROM Data LIMIT 2";
  7. if ($res = $mysqli->query($sql)) {
  8. if ($res->num_rows > 0) {
  9. echo "<table>";
  10. echo "<tr>";
  11. echo "<th>Firstname</th>";
  12. echo "<th>Lastname</th>";
  13. echo "<th>Age</th>";
  14. echo "</tr>";
  15. while ($row = $res->fetch_array()) {
  16. echo "<tr>";
  17. echo "<td>".$row['Firstname']."</td>";
  18. echo "<td>".$row['Lastname']."</td>";
  19. echo "<td>".$row['Age']."</td>";
  20. echo "</tr>";
  21. }
  22. echo "</table>";
  23. $res->free();
  24. }
  25. else {
  26. echo "No matching records are found.";
  27. }
  28. }
  29. else {
  30. echo "ERROR: Could not able to execute $sql. ".$mysqli->error;
  31. }
  32. $mysqli->close();

输出:

bf8ae206a311a53339d4bf5387fe739.png

示例3:使用PDO方法的Limit子句

  1. <?php
  2. try {
  3. $pdo = new PDO("mysql:host=localhost;dbname=Mydb", "root", "");
  4. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5. }
  6. catch (PDOException $e) {
  7. die("ERROR: Could not connect. ".$e->getMessage());
  8. }
  9. try {
  10. $sql = "SELECT * FROM Data LIMIT 2";
  11. $res = $pdo->query($sql);
  12. if ($res->rowCount() > 0) {
  13. echo "<table>";
  14. echo "<tr>";
  15. echo "<th>Firstname</th>";
  16. echo "<th>Lastname</th>";
  17. echo "<th>Age</th>";
  18. echo "</tr>";
  19. while ($row = $res->fetch()) {
  20. echo "<tr>";
  21. echo "<td>".$row['Firstname']."</td>";
  22. echo "<td>".$row['Lastname']."</td>";
  23. echo "<td>".$row['Age']."</td>";
  24. echo "</tr>";
  25. }
  26. echo "</table>";
  27. unset($res);
  28. }
  29. else {
  30. echo "No matching records are found.";
  31. }
  32. }
  33. catch (PDOException $e) {
  34. die("ERROR: Could not able to execute $sql. ".$e->getMessage());
  35. }
  36. unset($pdo);

输出:

159bb15415432bd87c408fcd0388a41.png

相关推荐:《mysql教程》

本篇文章就是关于mysql中limit用法详解,希望对需要的朋友有所帮助!

以上就是PHP mysql中limit用法详解(代码示例)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行