Hướng dẫn select first row mysql php - chọn hàng đầu tiên mysql php

10

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Đây là vấn đề của tôi. Tôi cần nhiều hơn một hàng từ cơ sở dữ liệu và tôi cần hàng đầu tiên cho một nhiệm vụ nhất định và sau đó đi qua tất cả danh sách để tạo một tập bản ghi.

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = //extract first row from database
//add display some field from it


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}

Bây giờ, làm thế nào để trích xuất chỉ hàng đầu tiên?

Đã hỏi ngày 6 tháng 1 năm 2011 lúc 11:52Jan 6, 2011 at 11:52

Hướng dẫn select first row mysql php - chọn hàng đầu tiên mysql php

2

Sử dụng mysql_fetch_assoc () không chỉ tìm được một hàng, nó còn di chuyển con trỏ bên trong của kết quả được đặt sang hàng tiếp theo. Để đặt lại tài nguyên kết quả về hàng đầu tiên, bạn cần sử dụng mysql_data_seek ().

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}

Đã trả lời ngày 6 tháng 1 năm 2011 lúc 12:22Jan 6, 2011 at 12:22

David Powersdavid PowersDavid Powers

1.62412 huy hiệu bạc11 huy hiệu đồng12 silver badges11 bronze badges

5

Nếu bạn muốn lấy tất cả các hàng từ hàng đầu tiên thì hãy thử các hàng sau

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}

Thông tin thêm về mysql_data_seek ở đây: PHP: mysql_data_seek - hướng dẫn sử dụng

Đã trả lời ngày 6 tháng 1 năm 2011 lúc 12:22Jan 6, 2011 at 12:22

David Powersdavid PowersAnush Prem

1.62412 huy hiệu bạc11 huy hiệu đồng8 silver badges16 bronze badges

1

Nếu bạn muốn lấy tất cả các hàng từ hàng đầu tiên thì hãy thử các hàng sauObject oriented style :

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = $result->fetch_assoc()){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while( $row = $result->fetch_assoc()) {
       //display all of them
    }
}

Thông tin thêm về mysql_data_seek ở đây: PHP: mysql_data_seek - hướng dẫn sử dụngApr 10, 2018 at 19:05

Hướng dẫn select first row mysql php - chọn hàng đầu tiên mysql php

Anush Premanush Prem

$result = mysql_query("...");
if ($row = mysql_fetch_assoc($result)) {
   $firstRow = $row;

   while ($row = mysql_fetch_assoc($result)) {
       // all the rest
   }
}

1.5218 huy hiệu bạc16 Huy hiệu đồng

Bạn có thể sử dụng kiểu định hướng đối tượng:Jan 6, 2011 at 11:53

Hướng dẫn select first row mysql php - chọn hàng đầu tiên mysql php

4

Để chỉ trả lại hàng đầu tiên phù hợp với truy vấn

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 của bạn, bạn cần thêm mệnh đề
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3 vào câu lệnh
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 của bạn.

Điều khoản

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3 được sử dụng để kiểm soát số lượng hàng được trả về bởi truy vấn của bạn. Khi bạn thêm
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
6 vào câu lệnh
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2, thì chỉ có một hàng sẽ được trả về.

Ở đây, một ví dụ về truy vấn

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 với mệnh đề
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3:

SELECT * FROM example_database LIMIT 1;

Điều khoản

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3 chấp nhận hai tham số:

  • $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    
    if ( $row = mysql_fetch_assoc ($result){
        $firstRow = $row;
        mysql_data_seek($result, 0);
    
        while($row = mysql_fetch_assoc($result)) {
           //display all of them
        }
    }
    
    1 của offset trước khi trả lại hàng (tùy chọn)
  • $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    
    if ( $row = mysql_fetch_assoc ($result){
        $firstRow = $row;
        mysql_data_seek($result, 0);
    
        while($row = mysql_fetch_assoc($result)) {
           //display all of them
        }
    }
    
    1 của các hàng để trả về (bắt buộc)

Vì phần bù là tùy chọn, bạn chỉ có thể chuyển một đối số cho mệnh đề để chỉ giới hạn số lượng hàng được trả về.

Ví dụ: giả sử bạn có bảng

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
3 với dữ liệu sau:

+----+---------+---------+-------+--------+
| id | name    | subject | score | gender |
+----+---------+---------+-------+--------+
|  1 | Mark    | Math    |     7 | male   |
|  2 | Natalia | Math    |     8 | female |
|  3 | Gary    | Math    |  NULL | male   |
|  4 | Joe     | English |     8 | male   |
|  5 | Sarah   | Math    |  NULL | female |
|  6 | Peter   | English |     6 | male   |
|  7 | Nathan  | English |     8 | male   |
+----+---------+---------+-------+--------+

Để chỉ trả về hàng đầu tiên, bạn cần thực hiện truy vấn SQL sau:

SELECT * FROM students LIMIT 1;

Bộ kết quả được trả về sẽ được hiển thị bên dưới:

+----+------+---------+-------+--------+
| id | name | subject | score | gender |
+----+------+---------+-------+--------+
|  1 | Mark | Math    |     7 | male   |
+----+------+---------+-------+--------+
1 row in set (0.00 sec)

Và đó là cách mà bạn có thể

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 chỉ hàng đầu tiên sử dụng MySQL.

Tiếp theo, hãy để xem cách bạn có thể thêm một đơn đặt hàng tùy chỉnh để thay đổi thứ tự của các hàng.

MySQL Chọn hàng đầu tiên từ một đơn đặt hàng tùy chỉnh

Hàng đầu tiên được trả về bởi câu lệnh

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 sẽ luôn tuân theo thứ tự bảng nội bộ được tạo bởi MySQL.

Thông thường, các hàng trong bàn của bạn đã được đặt hàng vào thời điểm chúng được chèn vào bàn.

Bạn có thể thay đổi thứ tự của các hàng được trả về bởi câu lệnh

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 bằng cách thêm mệnh đề
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
7.

Điều khoản

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
7 cho phép bạn sắp xếp kết quả được trả về được đặt theo giá trị của một cột theo thứ tự tăng dần (
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
9) hoặc giảm dần (
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = $result->fetch_assoc()){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while( $row = $result->fetch_assoc()) {
       //display all of them
    }
}
0).

Ví dụ: câu lệnh SQL sau đây sẽ sắp xếp kết quả bằng cột

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = $result->fetch_assoc()){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while( $row = $result->fetch_assoc()) {
       //display all of them
    }
}
1 theo thứ tự giảm dần:

SELECT * FROM students ORDER BY id DESC;

Thêm mệnh đề

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3 sau mệnh đề
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
7 để chỉ trả về hàng đầu tiên như sau:

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
0

Và đó là cách mà bạn có thể xác định một đơn đặt hàng tùy chỉnh và

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
2 chỉ hàng đầu tiên từ thứ tự đó bằng cách sử dụng từ khóa
$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}
3.

Bạn có thể thay đổi điều khoản

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = mysql_fetch_assoc ($result){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while($row = mysql_fetch_assoc($result)) {
       //display all of them
    }
}
7 để phù hợp với yêu cầu của bạn.

Làm cách nào để có được hàng đầu tiên trong MySQL?

Hàm đầu tiên MySQL được sử dụng để trả về giá trị đầu tiên của cột đã chọn.Ở đây, chúng tôi sử dụng mệnh đề giới hạn để chọn bản ghi đầu tiên trở lên ...
Chọn cột_name ..
Từ tablay_name ..
Giới hạn 1 ;.

Làm cách nào để chọn một hàng trong mysql?

Câu lệnh MySQL Chọn được sử dụng để truy xuất các hàng từ một hoặc nhiều bảng.Tuyên bố cũng có thể bao gồm các tuyên bố và phân nhóm công đoàn.Chọn câu lệnh được sử dụng để tìm nạp các hàng hoặc bản ghi từ một hoặc nhiều bảng.

Làm cách nào để chọn 5 hàng đầu tiên trong MySQL?

SQL TOP, GIỚI HẠN, LẠI ĐẦU TIÊN HOẶC ROWNUM..
SQL Server / MS Access Cú pháp: chọn Số hàng đầu | phần trăm cột_name (s) từ TABEL_NAME.....
Cú pháp MySQL: Chọn Cột_Name (S) từ Table_Name.....
Oracle 12 Cú pháp: Chọn cột_name (S) ....
Cú pháp oracle cũ hơn: Chọn cột_name (S) ....
Cú pháp Oracle cũ hơn (có thứ tự bởi): Chọn *.

Làm thế nào để bạn có được hàng đầu tiên của một bảng trong SQL?

Hàm đầu tiên () được sử dụng để trả về hàng đầu tiên của bất kỳ bảng nào. is used to return the first row of any table.