简体   繁体   中英

PHP loop using foreach

Here is the problem:-

I've got a table of results from sports matches called 'MatchResults' with the following fields

Player1 Player2 Player1GamesWon Player2GamesWon Player1Pts Player2Pts league_id

First I am running a query which will filter this table using a stored variable I already have, giving me a shorter collection of rows:-

$qry = "SELECT * FROM BadmintonMatchResults WHERE league_id = '$leagueid'";

I need to run through each row that is produced from the above query and use the values to update another table called 'LeagueTable'. The structure of LeagueTable is as follows:-

member_id Played Won Lost GamesWon GamesLost Difference Points

These updates take the form of several SQL queries, for example:-

$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p2id'");

if($p1pts > $p2pts) {
    $qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p1id'");
    $qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p2id'"); 
}
else if($p2pts > $p1pts) {
    $qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p2id'");
    $qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p1id'"); 
}

$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p1won' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p2won' WHERE member_id = '$p2id'");  
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p1won' WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p2won' WHERE member_id = '$p1id'");

$qry = mysql_query("UPDATE LeagueTable SET Difference = GamesWon - GamesLost");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p1pts' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p2pts' WHERE member_id = '$p2id'");

Ignore any $variables I have as I'm currently doing this off data entered in a form, but want to alter it so that it uses rows from another database instead. I also know that my queries can be condensed and combined, my main problem is looping round this collection of queries multiple times for different sets of values.

My questions are:-

  1. How to I reference the fields from the first table in the queries I need to run? Obviously just listing the field name 'Player1' isn't going to work.
  2. How do I loop through each row and run the above 2 queries along with 8 more similar ones.

Note: Some of these queries are already nested inside an IF statement so any loop would have to go outside of that.

Any help would be much appreciated!

If you are going to do things that way then please tell me that you are sanitising the data before you use it in SQL statements. If I types " 0' or 1=1 " as my input and you used that the SQL would not do what you expect it to do.

To lop and then use the data by reference name from one tot he next you would simply do this:

if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){

        // ... your loop here code here
        mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '" . $row['Player1'] . "'");

    }
}

To be honest this still looks like you've got a very inefficient way of doing things as you are making your MySQL socket work very hard. I would consider how much calculations you need to store and how many can be worked out in an aggregate SQL query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM