Jump to content

[SOLVED] Getting current row number - mysql_fetch_row


tlavelle

Recommended Posts

This code I wrote doesn't work but illustrates what I want to do

 

 while($row = mysql_fetch_row($metric_result)) {
        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>" . $row[1]."</td>";
        echo "<td>".$row[2]."</td>";
	echo "<td>".$myvalue[$row]."</td>";
        echo "</tr>";
    }

 

How do I generate an index for the $myvalue[] array based on the current mysql row?

Is your query only returning one result? If you change this:

while($row = mysql_fetch_row($metric_result)) {

 

to:

while($row = mysql_fetch_array($metric_result)) {

 

The code that follows will work, though I'm not exactly sure it is the values you want to return.

Try this:

 


$len = mysql_num_rows($metric_result);
for( $i = 0; $i < $len; ++$i )
{
    $row = mysql_fetch_row($metric_result);

    echo "<tr>";
      echo "<td>" . $row[0]. "</td>";
      echo "<td>" . $row[1]. "</td>";
      echo "<td>" . $row[2]. "</td>";
      echo "<td>" . $myvalue[$i] . "</td>";
    echo "</tr>";
}

 

By using a for loop your index ($i) is your current row number. Alternatively you could just add a counter to your while loop, but I think it's pertier this way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.