旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

PHP mysql中limit用法详解(代码示例)【MySQL教程】,mysql,limit

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:65评论:0


导读:在MySQL中,LIMIT子句与SELECT语句一同运用,以限定效果集合的行数。LIMIT子句接收一个或两个offset和count的参数。这两个参数的值都可所以零或正整数。...
在MySQL中,LIMIT子句与SELECT语句一同运用,以限定效果集合的行数。LIMIT子句接收一个或两个offset和count的参数。这两个参数的值都可所以零或正整数。

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

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

Limit子句接收一个或两个参数,当指定两个参数时,第一个参数是偏移量,第二个参数示意计数,而当只指定一个参数时,它示意从效果集最先返回的行数。

LIMIT语法:

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

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

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

SELECT * FROM Data LIMIT 3;

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

SELECT * FROM Data LIMIT 1, 2;

下面是PHP mysql完成查询的代码示例:

示例1:Limit前提

<?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);

输出:

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

每次挪用mysqli_fetch_array()时,它都邑从res()集合返回下一行。

while轮回用于遍历表“data”的一切行。

示例2:运用面向对象要领的Limit子句

<?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:运用PDO要领的Limit子句

<?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);

输出:

相干引荐:《mysql教程》

本篇文章就是关于mysql中limit用法详解,愿望对须要的朋侪有所协助!

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

标签:mysqllimit


欢迎 发表评论: