Mysqli获取数组第n行

时间:2012-08-25 23:03:12

标签: php mysql loops

我有以下代码来获取单行:

$query = "SELECT *
          FROM translations
          WHERE iddoc = '$id'
          AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);

我知道这在while循环中很有用,你可以循环遍历结果。

但我需要能够获取满足此条件的特定行。遵循这个伪代码的东西:

$query = "SELECT *
          FROM translations
          WHERE iddoc = '$id'
          AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)

依旧......

我该怎么做?

3 个答案:

答案 0 :(得分:5)

您可以尝试这样的事情

$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
   $arrayofrows = $row;
}

你现在可以拥有

$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)

答案 1 :(得分:2)

我认为您正在寻找的功能是 mysqli_fetch_all ,如下所示。这样就无需创建额外的循环结构或函数来执行已经提供的操作。

$query = "SELECT *
          FROM translations
          WHERE iddoc = '$id'
          AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)

答案 2 :(得分:1)

这取决于您是否需要整个结果集,但我认为LIMIT可以像:

$query = "SELECT *
          FROM translations
          WHERE iddoc = '$id'
          AND submitted = 1 LIMIT 200,200;";

否则,正如其他人所说,您将需要转换为数组(这是fetch_all所做的),然后从该数组中获取元素。