0

The output I get

Name    Age
ABC     12
PQR     40
XYZ     10

code for retrieving data from MySql database using PHP.

  //Get records from database
  $result = mysql_query("SELECT * FROM people;");

    //Add all records to an array
    $rows = array();
    $count = 0;
    while($row = mysql_fetch_array($result))
    {
     // Here i want to bind one more colomn for count as Row number to array
        count = count + 1;
        $rows[] = $row;
    }

desired output..

RowNo    Name    Age
 1       ABC     12
 2       PQR     40
 3       XYZ     10

I want to send count number with mysql data as one colomn. Can anybody guide me to on that?

3
  • don't use mysql_<> functions please Commented Sep 9, 2013 at 8:59
  • Then suggest an alternative instead of saying what not to use. Commented Sep 9, 2013 at 9:03
  • Similar question here: stackoverflow.com/questions/3126972/mysql-row-number (you don't have to iterate through anything in PHP, you can get it from mysql) Commented Sep 9, 2013 at 9:16

2 Answers 2

2
while($row = mysql_fetch_array($result))
{
    $count = $count + 1;
    $row['RowNo']= $count; //Just add this
    $rows[] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this

$counter = 0;
for($i = 0; $i < count($result); $i++)
{
   $counter++;
   $result[$i]['rowcount'] = $counter;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.