PHP | MySQL限制子句

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

null

抵消: 它用于指定要返回的第一行的偏移量。 计数: 它用于指定要返回的最大行数。

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

语法:

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

你们可以在本文中详细了解限制条款 MySQL |限制条款 .

让我们考虑下面的表“数据”,它有三列“FiestNew”、“LaSTNED”和“AGE”。 图片[1]-PHP | MySQL限制子句-yiteyi-C++库

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

SELECT * FROM Data LIMIT 3;

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

SELECT * FROM Data LIMIT 1, 2;

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

  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 "<table>" ;
    echo "<tr>" ;
    echo "<th>Firstname</th>" ;
    echo "<th>Lastname</th>" ;
    echo "<th>Age</th>" ;
    echo "</tr>" ;
    while ( $row = mysqli_fetch_array( $res )) {
    echo "<tr>" ;
    echo "<td>" . $row [ 'Firstname' ]. "</td>" ;
    echo "<td>" . $row [ 'Lastname' ]. "</td>" ;
    echo "<td>" . $row [ 'Age' ]. "</td>" ;
    echo "</tr>" ;
    }
    echo "</table>" ;
    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 );
    ? >

    
    

    输出: 图片[2]-PHP | MySQL限制子句-yiteyi-C++库

    说明:

    1. “res”变量存储函数mysql_query()返回的数据。
    2. 每次调用mysqli_fetch_array()时,它都会从res()集中返回下一行。
    3. while循环用于循环表“data”的所有行。
  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 "<table>" ;
    echo "<tr>" ;
    echo "<th>Firstname</th>" ;
    echo "<th>Lastname</th>" ;
    echo "<th>Age</th>" ;
    echo "</tr>" ;
    while ( $row = $res ->fetch_array()) {
    echo "<tr>" ;
    echo "<td>" . $row [ 'Firstname' ]. "</td>" ;
    echo "<td>" . $row [ 'Lastname' ]. "</td>" ;
    echo "<td>" . $row [ 'Age' ]. "</td>" ;
    echo "</tr>" ;
    }
    echo "</table>" ;
    $res ->free();
    }
    else {
    echo "No matching records are found." ;
    }
    }
    else {
    echo "ERROR: Could not able to execute $sql. " . $mysqli ->error;
    }
    $mysqli ->close();
    ? >

    
    

    输出: 图片[3]-PHP | MySQL限制子句-yiteyi-C++库

  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 "<table>" ;
    echo "<tr>" ;
    echo "<th>Firstname</th>" ;
    echo "<th>Lastname</th>" ;
    echo "<th>Age</th>" ;
    echo "</tr>" ;
    while ( $row = $res ->fetch()) {
    echo "<tr>" ;
    echo "<td>" . $row [ 'Firstname' ]. "</td>" ;
    echo "<td>" . $row [ 'Lastname' ]. "</td>" ;
    echo "<td>" . $row [ 'Age' ]. "</td>" ;
    echo "</tr>" ;
    }
    echo "</table>" ;
    unset( $res );
    }
    else {
    echo "No matching records are found." ;
    }
    }
    catch (PDOException $e ) {
    die ( "ERROR: Could not able to execute $sql. " . $e ->getMessage());
    }
    unset( $pdo );
    ? >

    
    

    输出: 图片[3]-PHP | MySQL限制子句-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享