简体   繁体   English

php-mysql如何在mysql中选择特定的行号?

[英]php-mysql How to select the particular number of the row in mysql?

I am new at this and learning the code. 我是新手,正在学习代码。 I want to create the php code which select the particular row. 我想创建选择特定行的php代码。 say 5th row or 6th row any row. 说第5行或第6行。 I create the code like this 我创建这样的代码

<?php
$servername = "localhost";
$username = "test1";
$password = "pass";
$dbname = "test1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT FIELD1, FIELD2 FROM mytable ";
$result = $conn->query($sql);

if ($result->num_rows > 0)


 {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["FIELD1"]. " - Name: " . $row["FIELD2"]. " <br>";
    }



} else 
{
    echo "0 results";
}
$conn->close();
?>

THis code works fine but it give the all data of the table.I want to just select particular row number data.how to do this?? 该代码可以正常工作,但它提供了表的所有数据。我只想选择特定的行号数据。如何做到这一点?

You can do it with the LIMIT statement, say LIMIT 3,1 to select the 4th row. 您可以使用LIMIT语句(例如LIMIT 3,1)来选择第4行。 First number is the starting row, second number is the count of rows to select. 第一个数字是起始行,第二个数字是要选择的行数。

$sql = "SELECT FIELD1, FIELD2 FROM mytable LIMIT $row_index, 1";

will give you row $row_index + 1 将给您行$ row_index + 1

您可以在查询中使用WHERE条件作为SELECT FIELD1, FIELD2 FROM mytable WHERE id = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM