我正在从一个api提取数据,该api输出包含玩家统计信息的json数据。对于我玩的游戏中的每种技能,我都有单独的表格。总共有27项技能,因此总共28个表(包括用户表)。首先有两个问题,一个是insert语句试图将同一条记录放入两次并创建重复的条目错误,第二个问题是某些insert语句没有在其各自的表中放入任何内容。这也是另一个发生的问题,那就是它仅将一个用户的统计信息放入数据库中。示例:我的用户表中有3个用户:Bob,Jane,Paul仅将Bobs技能数据放入。因此,当此时在users表中大约有150个名字时,基本上只插入一条记录。哦,我忘了提及我将用户表中的用户ID插入技能表中。我提供了$ highscores数组,以防万一有人需要
起初,我认为这是我多次使用$query
变量的事实,因此我使它们变得唯一,例如:$query1, $query2, $query3 etc
,问题仍然存在。我检查了所有的拼写,如果if正在检查变量是否等于一项技能,并且所有拼写都匹配。所有代码都是相同的。
Array
(
[skillvalues] => Array
(
[0] => Array
(
[level] => 99
[xp] => 648475179
[rank] => 41501
[id] => 3
)
.
.
.
repeat for the other 26 skills
)
)
//grab users and the users id out of the users table
$query = $conn->query("SELECT user_id, name FROM users ORDER BY user_id
ASC");
ini_set('max_execution_time', 1600);
While($row = $query->fetch(PDO::FETCH_ASSOC)){
$name = $row["name"];
$user_id = $row["user_id"];
//to compensate for spaces because otherwise file_get_contents will fail
$name = str_replace(' ', '%20', $name);
//api where the data is coming from which outputs as json
$highscores = file_get_contents("https://apps.runescape.com/runemetrics/profile/profile?user=" .$name);
$highscores = json_decode($highscores, true);
$offset = 8;
//only grab the skills data
$highscores = array_slice($highscores, $offset);
//put the space back
$name = str_replace('%20', ' ', $name);
//assigns skill name to skill id
$skills_arr = array('0' => "attack",'1' => "defence",'2' => "strength",'3' => "hitpoints",'4' => "ranged",'5' => "prayer",'6' => "magic",'7' => "cooking",'8' => "woodcutting",'9' => "fletching",'10' => "fishing",'11' => "firemaking",'12' => "crafting",'13' => "smithing",'14' => "mining",'15' => "herblore",'16' => "agility",'17' => "thieving",'18' => "slayer",'19' => "farming",'20' => "runecrafting",'21' => "hunter",'22' => "construction",'23' => "summoning",'24' => "dungeoneering",'25' => "divination",'26' => "invention");
//to catch if file_get_contents ends up grabing a profile the player has thier data set to private
if(!empty($highscores)){
foreach($highscores['skillvalues'] as $skills){
$skill = $skills_arr[$skills['id']];
$xp = $skills['xp'];
$rank = $skills['rank'];
$level = $skills['level'];
$profile = "1";
//to catch if file_get_contents ends up grabbing a profile where the player has no rank in any skill
if(!empty($rank)){
//assign the values to the correct skill
if($skill == "attack"){
$skill = $skill;
$rank = $rank;
$level = $level;
$xp = $xp;
$query = $conn->prepare("INSERT INTO $skill VALUES(?, ?, ?, ?, ?)");
$query->execute(array($user_id, $rank, $level, $xp, $profile));
}
.
.
.
repeat the exact same code for the other 26 skills
}
}//END WHILE LOOP