Foreach刚刚回应首次参赛

时间:2010-10-29 12:54:45

标签: php while-loop

道歉,如果我应该知道更好,但我一直在努力与这一个。

我有一个300行的mysql数据库。它包含4列,“eventid”,“player1”,“player2”,“得分”。在游戏中,玩家1给予其他人(玩家2)100分的分数。

我要做的是向登录用户(player1)显示他们已经得分的“player2”表。

我的代码如下所示:

$currentuserid = 00001;

$opponent_data = mysql_query("SELECT * FROM `scores` WHERE `player1` = $currentuserid ORDER by score");

$opponent_count = mysql_num_rows($opponent_data);

    echo $opponent_count.'<br>'; // Just to test -> and it shows I have 144 entries in the array, i.e. 144 player 2's that player 1 has scored

$opponent_scores = mysql_fetch_assoc($opponent_data);

$runrows = $opponent_scores;

foreach ($opponent_scores as &$runrows);

    {
    $id = $runrows['eventid'];
    $player2 = $runrows['player2'];
    $score = $runrows['score'];


    echo $player2." got ".$score;

    echo "<br>";

    }

当我运行这一切时,我能看到的是

  

144

     

73得到44

但我希望看到144排“玩家2”获得“玩家2的分数”。

我做错了什么?

5 个答案:

答案 0 :(得分:9)

for-each循环后你有一个分号;不应该在那里。

答案 1 :(得分:6)

另外:mysql_fetch_assoc只返回指向结果集中第一行的指针。这就是为什么你最终只会打印一行。

将您的代码更改为:

while($opponent=mysql_fetch_assoc($opponent_data)) {
     echo $opponent['player2']." got ".$opponent['score'];
}

答案 2 :(得分:1)

看起来每个语句之后都有一个异常的分号

答案 3 :(得分:1)

$currentuserid = 00001;

$opponent_data = mysql_query("SELECT * FROM `scores` WHERE `player1` = $currentuserid ORDER by score");

$opponent_count = mysql_num_rows($opponent_data);

    echo $opponent_count.'<br>'; // Just to test -> and it shows I have 144 entries in the array, i.e. 144 player 2's that player 1 has scored

$opponent_scores = mysql_fetch_assoc($opponent_data);

foreach ($opponent_scores as $row);

    {
    $id = $row['eventid'];
    $player2 = $row['player2'];
    $score = $row['score'];


    echo $player2." got ".$score;

    echo "<br>";

    }

答案 4 :(得分:1)

mysql_fetch_assoc返回单个记录的关联数组。

查看文档,了解您应该做什么的示例:http://uk3.php.net/mysql_fetch_assoc

while ($row = mysql_fetch_assoc($opponent_scores)) {
    // var_dump($row);
}