Jump to content

[SOLVED] Getting current row number - mysql_fetch_row


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.