Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqli fetch array nth row

Tags:

loops

php

mysql

I have the following code that fetches a single row:

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

I know this is useful in a while loop, where you can just loop through the results.

But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:

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

and so on...

How can I do this?

like image 637
sir_thursday Avatar asked Aug 25 '12 23:08

sir_thursday


1 Answers

You can try something like this

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

You can now have

$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
like image 115
codingbiz Avatar answered Oct 14 '22 04:10

codingbiz