📜  PHP | MySQL 限制条款

📅  最后修改于: 2022-05-13 01:55:15.750000             🧑  作者: Mango

PHP | MySQL 限制条款

在 MySQL 中, LIMIT子句与 SELECT 语句一起使用,以限制结果集中的行数。限制子句接受一个或两个参数,它们是偏移量和计数。两个参数的值可以是零或正整数。

偏移量:用于指定要返回的第一行的偏移量。
Count:用于指定要返回的最大行数。

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

句法:

SELECT column1, column2, ...
FROM table_name
LIMIT offset, count;

您可以在文章 MySQL | 中详细了解 LIMIT 子句。限制条款。

让我们考虑下表“数据”,其中包含“名字”、“姓氏”和“年龄”三列。

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

SELECT * FROM Data LIMIT 3;

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

SELECT * FROM Data LIMIT 1, 2;

下面是查询的PHP实现,用于在过程和面向对象的扩展中使用 LIMIT 子句显示表“数据”的前两行:

  1. 使用程序方法的限制条款
    < ? php $link = mysqli_connect("localhost", "root", "", "Mydb");
      
    if ($link == = false) {
        die("ERROR: Could not connect. ".mysqli_connect_error());
    }
      
    $sql = "SELECT * FROM Data LIMIT 2";
    if ($res = mysqli_query($link, $sql)) {
        if (mysqli_num_rows($res) > 0) {
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            while ($row = mysqli_fetch_array($res)) {
                echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            }
            echo "
    FirstnameLastnameAge
    ".$row['Firstname']."".$row['Lastname']."".$row['Age']."
    ";         mysqli_free_result($res);     }     else {         echo "No matching records are found.";     } } else {     echo "ERROR: Could not able to execute $sql. ".mysqli_error($link); }    mysqli_close($link); ? >

    输出 :

    解释:

    1. “res”变量存储由函数mysql_query() 返回的数据。
    2. 每次调用 mysqli_fetch_array() 时,它都会返回 res() 集中的下一行。
    3. while 循环用于循环遍历表“数据”的所有行。
  2. 使用面向对象方法的限制子句
    < ? php $mysqli = new mysqli("localhost", "root", "", "Mydb");
      
    if ($mysqli == = false) {
        die("ERROR: Could not connect. ".$mysqli->connect_error);
    }
      
    $sql = "SELECT * FROM Data LIMIT 2";
    if ($res = $mysqli->query($sql)) {
        if ($res->num_rows > 0) {
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            while ($row = $res->fetch_array()) {
                echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            }
            echo "
    FirstnameLastnameAge
    ".$row['Firstname']."".$row['Lastname']."".$row['Age']."
    ";         $res->free();     }     else {         echo "No matching records are found.";     } } else {     echo "ERROR: Could not able to execute $sql. ".$mysqli->error; }    $mysqli->close(); ? >

    输出 :

  3. 使用 PDO 方法的限制条款
    < ? php try {
        $pdo = new PDO("mysql:host=localhost;dbname=Mydb", "root", "");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $e) {
        die("ERROR: Could not connect. ".$e->getMessage());
    }
      
    try {
        $sql = "SELECT * FROM Data LIMIT 2";
        $res = $pdo->query($sql);
        if ($res->rowCount() > 0) {
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            echo "";
            while ($row = $res->fetch()) {
                echo "";
                echo "";
                echo "";
                echo "";
                echo "";
            }
            echo "
    FirstnameLastnameAge
    ".$row['Firstname']."".$row['Lastname']."".$row['Age']."
    ";         unset($res);     }     else {         echo "No matching records are found.";     } } catch (PDOException $e) {     die("ERROR: Could not able to execute $sql. ".$e->getMessage()); }    unset($pdo); ? >

    输出 :