pdo mysql limit_PHP mysql中limit用法详解(代码示例)

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

87072b48011b24e871b79520f2ca478c.png

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

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

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

LIMIT语法:SELECT column1, column2, ...

FROM table_name

LIMIT offset, count;

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

3588e1f7022666d9ce4a7946aad1a49f.png

要从“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 "

echo "

";

echo "

Firstname";

echo "

Lastname";

echo "

Age";

echo "

";

while ($row = mysqli_fetch_array($res)) {

echo "

";

echo "

".$row['Firstname']."";

echo "

".$row['Lastname']."";

echo "

".$row['Age']."";

echo "

";

}

echo "

";

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

输出:

19ffee001cef9c3a64bdfcaa63c27670.png

注:“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 "

echo "

";

echo "

Firstname";

echo "

Lastname";

echo "

Age";

echo "

";

while ($row = $res->fetch_array()) {

echo "

";

echo "

".$row['Firstname']."";

echo "

".$row['Lastname']."";

echo "

".$row['Age']."";

echo "

";

}

echo "

";

$res->free();

}

else {

echo "No matching records are found.";

}

}

else {

echo "ERROR: Could not able to execute $sql. ".$mysqli->error;

}

$mysqli->close();

输出:

334441ef1e5f4f15fb444a90b6bf3cbc.png

示例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 "

echo "

";

echo "

Firstname";

echo "

Lastname";

echo "

Age";

echo "

";

while ($row = $res->fetch()) {

echo "

";

echo "

".$row['Firstname']."";

echo "

".$row['Lastname']."";

echo "

".$row['Age']."";

echo "

";

}

echo "

";

unset($res);

}

else {

echo "No matching records are found.";

}

}

catch (PDOException $e) {

die("ERROR: Could not able to execute $sql. ".$e->getMessage());

}

unset($pdo);

输出:

35f7383a16da05118c1df05fdbe23d07.png

相关推荐:《mysql教程》

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值