天天看點

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

點選藍字

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

關注我們!每天擷取最新的程式設計小知識!

源 / php中文網      源 / www.php.cn

在MySQL中,LIMIT子句與SELECT語句一起使用,以限制結果集中的行數。LIMIT子句接受一個或兩個offset和count的參數。這兩個參數的值都可以是零或正整數。

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

(檢視原文請點選本文末尾左下角:

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

Count:用于指定要傳回的最大行數。

Limit子句接受一個或兩個參數,當指定兩個參數時,第一個參數表示偏移量,第二個參數表示計數,而當隻指定一個參數時,它表示從結果集開始傳回的行數。

LIMIT文法:

SELECT column1, column2, ...

FROM table_name

LIMIT offset,

count

;

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

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

要從“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

);

輸出:

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

注:“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();

輸出:

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

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

);

輸出:

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

本篇文章就是關于mysql中limit用法詳解,希望對需要的朋友有所幫助!

mysql limit用法_PHP mysql中limit用法詳解(代碼示例)

-END-

聲明:本文選自「 php中文網 」,搜尋「 phpcnnew 」即可關注!