Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何计算行中的字段数?_Php_Count_Row - Fatal编程技术网

Php 如何计算行中的字段数?

Php 如何计算行中的字段数?,php,count,row,Php,Count,Row,如何计算此行中有多少字段: $row['number'] while ($row = mysql_fetch_array($result)) { echo ' <td class="alt">'.$row['number'].'</td> $number = $row['number'] } $row['number'] while($row=mysql\u fetch\u数组($result)) { 回声' “.$row['number']”

如何计算此行中有多少字段:

$row['number']
while ($row = mysql_fetch_array($result))
{
    echo '
    <td class="alt">'.$row['number'].'</td>
    $number = $row['number']
}
$row['number']
while($row=mysql\u fetch\u数组($result))
{
回声'
“.$row['number']”
$number=$row['number']
}

这可能取决于您如何填充
$row
。如果您使用
mysql\u fetch\u assoc()
mysql\u fetch\u row()
您只需使用
count($row)
。但是,如果使用
mysql\u fetch\u array()
则需要除以2,因为它同时返回枚举值和关联值


还有无数其他填充
$row
的方法。这只是猜测,没有更多的信息

这可能取决于您如何填充
$row
。如果您使用
mysql\u fetch\u assoc()
mysql\u fetch\u row()
您只需使用
count($row)
。但是,如果使用
mysql\u fetch\u array()
则需要除以2,因为它同时返回枚举值和关联值


还有无数其他填充
$row
的方法。这只是猜测,没有更多的信息

尝试使用
mysql\u num\u fields()
。 例如:


尝试使用
mysql\u num\u fields()
。 例如:



generaly,count($row['number'])如果它是一个数组-但这不是这里的解决方案-如果你不提供更多代码,我们无法给你答案。generaly,count($row['number'])如果它是一个数组-但这不是这里的解决方案-我们无法给你答案,如果你不提供更多的代码。如何解决这样的问题:查找填充$row的函数。在您的例子中,它是mysql\u fetch\u array()。从文档(和名称)中可以看出,这将返回一个数组。所以您真正要问的问题是“如何在PHP中计算数组中的元素?”。如果你用谷歌搜索这个问题,你会得到答案:count()。所以理解返回类型是使PHP开发更容易的关键。如何解决这样的问题:查找填充$row的函数。在您的例子中,它是mysql\u fetch\u array()。从文档(和名称)中可以看出,这将返回一个数组。所以您真正要问的问题是“如何在PHP中计算数组中的元素?”。如果你用谷歌搜索这个问题,你会得到答案:count()。因此,理解返回类型是使PHP开发更容易的关键。
<?php
$result = mysql_query("SELECT `field1`,`field2` FROM `table`");
/* returns 2 because field1, field2 === two fields */
echo mysql_num_fields($result);
?>