PHP循环遍历成员的问题

时间:2011-09-22 21:58:25

标签: php mysql

我的更新脚本有问题,基本上如果我将该行保留为mysql_fetch_row,那么它似乎只想将所有其他成员插入到数据库中,但是如果我将其更改为mysql_fetch_array则会将每个成员插入两次,但仍然不是数据库中的所有用户。我刚刚展示了下面脚本的开头,因为我很确定这必须是错误的位置。 任何帮助都会很棒。欢呼声。

    $result = mysql_query("SELECT member_id FROM members ORDER BY member_id");

    while ($member = mysql_fetch_array($result)){
        foreach ($member as &$member_id){

            $results_query = mysql_query("SELECT driver1 as driver1, driver2 as driver2, driver3 as driver3, driver4 as driver4, team as team, engine as engine, total_points as total FROM members WHERE member_id = '$member_id'")
            or die ("Failed to update" . mysql_error());

$userteam = mysql_fetch_assoc($results_query);

 //this is the bottom of the script after calculations have take place and the insert into the database//

 $results_query = mysql_query("INSERT INTO member_results (member_id, track_id, driver1, driver2, driver3, driver4, team, engine, driver1_points, driver2_points, driver3_points, driver4_points, team_points, engine_points, qualifying_points, race_points, total_points)
            VALUES ('$member_id', '$track', '$userteam[driver1]', '$userteam[driver2]', '$userteam[driver3]', '$userteam[driver4]', '$userteam[team]', '$userteam[engine]', '$userpoints[driver1]', '$userpoints[driver2]', '$userpoints[driver3]', '$userpoints[driver4]', '$userpoints[team]', '$userpoints[engine]', '$userpoints[qualifying]', '$userpoints[race]', '$userpoints[total]')")
            or die ("Failed to update" . mysql_error());

            $userteam["total"] += $userpoints["total"];

            $results_query = mysql_query("UPDATE members SET total_points = '$userteam[total]' WHERE member_id = '$member_id'")
            or die ("Failed to update" . mysql_error());


        }
    }

3 个答案:

答案 0 :(得分:0)

使用mysql_fetch_array时,不需要foreach循环。你输入两次查询的原因是因为mysql_fetch_array为行数据提取了一个关联数组和一个数值数组,所以它正在查看它们并插入数据两次,每个数组一个。

出于这个原因,

$member不需要foreach。 $ member只是一行被拉,而不是整个数据集。关联数组将列名称作为数组键返回。

   while ($member = mysql_fetch_array($result)){

                $results_query = mysql_query("SELECT driv1 as drivone, driv2 as drivtwo, 
driv3 as drivthree, driv4 as drivfour, team as teamone, 
eng as engone, total_points as total FROM members WHERE member_id = '$member['member_id']'") // If member_id is the column name for the ID
                or die ("Failed to update" . mysql_error()); 

答案 1 :(得分:0)

mysql_fetch_array返回多个数组(关联数组,数字数组或两者) 这就是为什么它在你的foreach中插入两次,并且可能是你的脚本破坏的原因。

尝试使用var_dumping $ member来查看可用于优化foreach的语法

请参阅此处的文档:
http://php.net/manual/en/function.mysql-fetch-array.php

答案 2 :(得分:0)

使用mysql_fetch_assoc($ result)或mysql_fetch_array($ result,MYSQL_ASSOC);

默认情况下mysql_fetch_array返回:

  • NUM索引(数组(0 => ...,1 => ...)),
  • 还合并了assoc indexation(array('fieldname'=> ...,'colname'=> ...))

所以你的代码看起来像: **请注意,已删除了foreach。

    $result = mysql_query("SELECT member_id FROM members ORDER BY member_id");

    while ($member = mysql_fetch_assoc($result)){
        $member_id = $member['member_id'];
        //foreach ($member as &$member_id){

            $results_query = mysql_query("SELECT driver1 as driver1, driver2 as driver2, driver3 as driver3, driver4 as driver4, team as team, engine as engine, total_points as total FROM members WHERE member_id = '$member_id'")
            or die ("Failed to update" . mysql_error());

$userteam = mysql_fetch_assoc($results_query);
//...