Eric Brotto
Eric Brotto

Reputation: 54281

Iterate through mysql result with a for loop

Normally when you iterate through a mysql result you do something like this:

    while ($row = mysql_fetch_assoc($result)) {
        echo $row["some argument"];
    }

How do you do this with a for loop? This would be helpful so I can iterate through a certain portion of my results. I'm looking for something like:

 for($i = 5; $i < 10; $i++){
    echo $row[$i]["some argument"];
 }

Thanks!


EDIT 1

Just to clarify, I need to loop through a portion of the results, which may mean rows 5 to 10 for example.

Also my result is a portion of rows from a table, not a table in it's entirety, so I would think that changing the my_sql query should not be a part of the answer.

Upvotes: 2

Views: 9958

Answers (6)

Vijeenrosh P.W
Vijeenrosh P.W

Reputation: 369

if you are wishing to wade through any particlar fields of subranges of rows then you can consider

 mysql_result($result,$row_num,$field)

routine

it returns the field attributes value of row_num'th row in result

say if you want to get "some argument" field's value for an subrange of results rows you can considier following snippet

$result = mysql_query("some query");
$num_rows = mysql_num_rows($result);
for($i=5;$i<$num_rows;$i++) {   // here I have used range 5 to max of result rows
    echo mysql_result($result,$i,"some argument"); 
}

Upvotes: 2

Christofer Eliasson
Christofer Eliasson

Reputation: 33875

I believe you could use the mysql_result() like this:

// Do your query here
$result = mysql_query("SELECT * FROM your_table");

for($i = 5; $i < 10; $i++){
   echo mysql_result($result, $i, "some argument");
}

Edit Noticed that my PHP-syntax was a bit off, corrected now.

Upvotes: 7

Eugen Rieck
Eugen Rieck

Reputation: 65342

If you only need a forward running cursor, you would

for(int i = 0; i < 10; i++){
    $row = mysql_fetch_assoc($result
    if (!$row) break;
    echo $row["some argument"];
}

If you need a seekable cursor you need to pull the result into an array;

$rows=array();
while ($row = mysql_fetch_assoc($result)) {
    $rows[]=$row;
}

//.. later, maybe repeatedly

for(int i = 0; i < 10; i++){
    echo $rows[i]["some argument"];
}

Upvotes: 1

Nikoloff
Nikoloff

Reputation: 4160

There are lots of ways to do this, but if you just want to iterate over all returned rows using while is the simplest solution. Here's another:

$count = mysql_num_rows($result);

 for(int i = 0; i < $count; i++){
$row = mysql_fetch_assoc($result);
    echo $row["some argument"];
 }

Upvotes: 1

dev-null-dweller
dev-null-dweller

Reputation: 29492

Like this:

 for(int i = 0; i < 10; i++){
    $row = mysql_fetch_assoc($result)
    echo $row["some argument"];
 }

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270775

You can load the whole result set into an array, and access it as you ask:

$all_results = array();
while ($row = mysql_fetch_assoc($result)) {
    // Append all rows to an array
    $all_results[] = $row;
}

Now you can access it as:

echo $all_results[3]['somecolumn'];

Or in a for loop:

for($i = 0; $i < 10; $i++){
  echo $all_results[$i]["somecolumn"];
}

Upvotes: 4

Related Questions