PHP计数MYSQL行问题?

时间:2013-03-25 03:21:09

标签: php mysql rows counting

如果我为一个人创建了至少一个团队,那么我将努力实现它,它将显示团队。如果他们没有团队,就会说没有团队。如果这个人至少拥有一个团队,那就有效,但是如果这个人不在团队中,则没有任何表现。我该如何解决这个问题?

<?php

                        $sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
                                $num = 0;
                                while($row = mysql_fetch_assoc($sql)){
                                    $num = ++$num;
                        $amount1 = mysql_num_rows($sql);
                        $name = $row["name"];
                                    $teamrank = $row["rank"];
                                    $teamlink = $row["link"];
                                    $players = $row["players"];
                        $teamid = $row['id'];

                        if($amount1 < 1){
                          $teams = "No Teams";
                          echo "$amount";
                        }else{
                          $teams = "$name";
                          echo "<a href='$teamlink?id=$teamid'>$teams</a>";
                        }


                      }print "$amount1";
                    ?>

2 个答案:

答案 0 :(得分:0)

请查看此代码的第三行 - 将其放在您的代码中,而不是您的行。

                        if($amount1 < 1){
                          $teams = "No Teams";
                          echo $teams;
                        }else{
                          $teams = "$name";
                          echo "<a href='$teamlink?id=$teamid'>$teams</a>";
                        }

答案 1 :(得分:0)

将计数放在while ... loop

之外
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");

$amount1 = mysql_num_rows($sql);  //<---- this should fix it

$num = 0;        
while($row = mysql_fetch_assoc($sql)){
    $num = ++$num;

    $name = $row["name"];
    $teamrank = $row["rank"];
    $teamlink = $row["link"];
    $players = $row["players"];

    $teamid = $row['id'];

    $teams = "$name";
    echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}

if($amount1 < 1){
   $teams = "No Teams";
}

print "$amount1";
?>