如何在数组中存储一个或多个包含mysql数据的列

时间:2013-09-09 08:57:44

标签: php mysql

我得到的输出

Name    Age
ABC     12
PQR     40
XYZ     10

使用PHP从MySql数据库检索数据的代码。

  //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;
    }

期望的输出..

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

我想用mysql data as one colomn发送计数。有人可以指导我吗?

2 个答案:

答案 0 :(得分:2)

while($row = mysql_fetch_array($result))
{
    $count = $count + 1;
    $row['RowNo']= $count; //Just add this
    $rows[] = $row;
}

答案 1 :(得分:0)

试试这个

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