需要计算在PHP中创建的列表的结果

时间:2015-09-14 16:58:04

标签: php list count

好的,我有一个列表被填充并在页面上回显。

$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);

while($record = mysql_fetch_array($mydata)){

echo "<td>" . $record['units'] . "</td>";

现在结果会根据'mech_units'的数量而波动。我需要的是显示列表中显示的数量。有什么建议吗?

2 个答案:

答案 0 :(得分:4)

您可以使用内置函数mysql_num_rows($mydata)。这将为您提供获取的记录总数。

答案 1 :(得分:1)

首先,我建议使用mysqli。

每次回显'mech_unit'时,你都可以声明一个增加1的变量。

$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);

$i = 0;

while($record = mysql_fetch_array($mydata)){

    $i++;

    echo "<td>" . $record['units'] . "</td>";

}

echo "There are " . $i . " mech_units.";

另一种选择是使用mysql_num_rows()函数。