Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am fetching data from MySQL database... I want to check index of each row... e.g

while($row=mysql_fetch_array($result)
{
i want index of $row in each row... (current Index).
}

Please Help me. Thanks

share|improve this question
3  
$i = 0; before the loop and $i++; in the end of the loop body – zerkms Sep 25 '11 at 11:55

closed as not a real question by Gordon, Your Common Sense, Shef, Simone Carletti, templatetypedef Sep 25 '11 at 18:08

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

3 Answers

up vote 1 down vote accepted

If you want the index from the database:

<?php
...
$result = mysql_query("SELECT * FROM whatever");

while($row = mysql_fetch_array($result)) {
    echo $row['index']; 
   # Where index is the field name containing the item ID from your database
}
...
?>

Or, if you want to count the number of items based on the output:

<?php
..
$result = mysql_query("SELECT * FROM whatever");
$index = 0;

while($row = mysql_fetch_array($result)) {
    echo $index.' '.$row['whatever'];
    $index++;
}
..
?>

That's what I understood from your question..

share|improve this answer

If I understood you correctly you want something like this

<?PHP
$i=0;
while($someThingIsTrue) {
   echo $i;
   $i++;
}
?>
share|improve this answer

$row is not a member of any array. So, it has no index at all.
And, I suppose, you don't need it either

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.