Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Index of an array in php while loop [closed]

Tags:

php

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

like image 539
zzzzz Avatar asked Oct 18 '25 21:10

zzzzz


1 Answers

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

like image 123
Andrei Durduc Avatar answered Oct 20 '25 11:10

Andrei Durduc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!