MYSQL PHP:我怎么能找到总数。 连续的列?

MYSQL PHP:我怎么能找到总数。 连续的列?

问题描述:

$result = mysql_query("select * from formtable") or die("Records could not be fetched");
    while($row=mysql_fetch_row($result))  
    { 
           echo $row[0].$row[1].$row[2];
    }

The above code runs perfect. But i find this statement -> echo $row[0].$row[1].$row[2] bit too static. Because if a new column is added i will have to modify the code as echo $row[0].$row[1].$row[2].$row[3].

So, How can i count total columns of the row in order to avoid such a problem?

  $ result = mysql_query(“select * from formtable”)或die(“无法获取记录”  “); 
 while($ row = mysql_fetch_row($ result))
 {
 echo $ row [0]。$ row [1]。$ row [2]; 
} 
  code>  
 
 

以上代码运行完美。 但我发现这个陈述 - > echo $ row [0]。$ row [1]。$ row [2] code>位太静。 因为如果添加了新列,我将不得不将代码修改为 echo $ row [0]。$ row [1]。$ row [2]。$ row [3] code>。 p>

那么,我如何计算行的总列数以避免出现这样的问题? p> div>

Better to use something like:-

foreach($row AS $column)
{
echo $column;
}

mysql_num_fields() is what you're looking for.

Just to not leave it unspoken; the mysql_* family functions are deprecated, for new code you should use mysqli or PDO.

You can use count($row) to find the number of fields. But it is better to use a foreach loop as previously suggested.

Wouldn't it make more sense to use something like this:

echo implode($row);

No need to use a loop when there's a function that does it for you...