I'm currently building a small site to retrieve player stats for a computer game from a mysql db and display them online.
I get player statistics for a list of players from a third party API and am trying to insert them into a table in the db. However my code is performing the inserts twice - the primary key restriction on the table stops duplicate entries, but I don't want to accept this.
Something is amiss with my looping logic, but I'm going in my own loop trying to figure this one out.
Sequence of events is:
- I query my db to get the player ID's needed for the API calls
- I put these in an array
- I query the third party api in a loop to get all the player stats
I want to insert the stats (1 row per player) to my db (I plan to escape the strings etc, it's a work in progress)
if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $member_data[] = $row; } foreach ($member_data as $id) { $endpoint = "http://www.bungie.net/Platform/Destiny/Stats/2/".$id[destiny_membership_id]."/".$id[destiny_character_id]."/"; $bungie_result = hitBungie($endpoint); $response = json_decode($bungie_result, true); $destiny_membership_id = $id[destiny_membership_id]; $destiny_character_id = $id[destiny_character_id]; $kills_deaths_ratio = $response[Response][allPvP][allTime][killsDeathsRatio][basic][displayValue]; // DB insert $sql = "INSERT INTO xax_pvp_stats (destiny_membership_id,destiny_character_id,kills_deaths_ratio) "; $sql .= "VALUES ('$destiny_membership_id','$destiny_character_id','$kills_deaths_ratio') "; $result = $conn->query($sql); if ($conn->query($sql) === FALSE) { echo "<br />Error: " . $sql . "<br />" . $conn->error; } else { echo $destiny_character_id." is in the DB"; } } } else { echo "0 results"; }