I'm trying to insert data from a steam user with CS:GO stats into a MySQL Database using PHP. I need the name
and value
values from the stats
array inserted into the MYSQL which I'm going to display in a Android application but I am unsure of how exactly to do it.
我正在尝试使用PHP将具有CS:GO统计数据的Steam用户的数据插入到MySQL数据库中。我需要插入到MYSQL中的stats数组中的名称和值,我将在Android应用程序中显示,但我不确定如何做到这一点。
Code:
码:
<?php
$con = new mysqli('mysql6.000webhost.com','a2689951_Cheesy','ansmh1997', 'a2689951_mSicx');
if (mysqli_connect_error()) {
echo mysqli_connect_error();
exit;
}
$stats = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=<key>&steamid=76561198050187807");
$json = json_decode($stats, true);
$stats = array();
foreach ($content['playerstats']['stats'] as $stat) {
$stats[$stat["name"]] = $stat["value"];
}
$insert = "INSERT INTO user (total_kills, total_deaths) VALUES (?, ?)";
$stmt = $con->prepare($insert);
$stmt->bind_param("ss", $total_kills, $total_deaths);
?>
API example output:
API示例输出:
{
"playerstats": {
"steamID": "76561198050187807",
"gameName": "ValveTestApp260",
"stats": [
{
"name": "total_kills",
"value": 35179
},
{
"name": "total_deaths",
"value": 30241
},
{
"name": "total_time_played",
"value": 2444377
},
// ...
]
}
}
2 个解决方案
#1
0
A couple of things to get into the habit of doing, since you're a beginner:
因为你是一个初学者,有几件事要养成做的习惯:
- Don't use the
mysql
-functions in PHP. Usemysqli
instead (it even supports object-orientet notation!) - 不要在PHP中使用mysql函数。使用mysqli(它甚至支持object-orientet表示法!)
- The way you have your
INSERT
-statement set up makes your vornable to SQL Injection attacks. Use a "prepared statement" instead. - 设置INSERT语句的方式使您可以进行SQL注入攻击。请改用“预备声明”。
- To "return" data from a PHP script (that is, send it directly to the browser), you need to use
echo
instead ofreturn
. - 要从PHP脚本“返回”数据(即将其直接发送到浏览器),您需要使用echo而不是return。
- You have this line in your code:
json_encode($response)
, where$response
is an empty array. This line won't do anything. thejson_encode()
-function returns a String that contains the JSON-representation of whatever you give it. It does not change it! - 你的代码中有这一行:json_encode($ response),其中$ response是一个空数组。这条线不会做任何事情。 json_encode() - 函数返回一个String,其中包含您提供的任何内容的JSON表示。它没有改变它!
- I removed the API-Key from the URL you had in your example, since sharing the key publicly is not a good idea and can result in Valve banning your key (for example if malicious people use it to send requests to the API).
- 我从您示例中的URL中删除了API-Key,因为公开共享密钥并不是一个好主意,并且可能导致Valve禁止您的密钥(例如,如果恶意用户使用它向API发送请求)。
Now for your actual problem:
现在针对您的实际问题:
foreach ($json->playerstats->stats as $stat) {
$stats[$stat["name"]] = $stat["value"];
$total_kills = $stats["total_kills"];
$total_deaths = $stats["total_deaths"];
I think there might be some confusion as to what part of this code is actually repeated by the loop. It's not just the first line, it's the entire body (enclosed by the {}
-brackets).
我认为这个代码的哪一部分实际上被循环重复可能会有些混乱。它不仅仅是第一行,它是整个主体(由{} -brackets包围)。
The problem is that you will only get the first entry from the JSON into the $stats
-array, which would be total_kills
. But you want all of them, so your loop should run over this part alone:
问题是你只能从JSON获得第一个条目到$ stats-array,这将是total_kills。但是你想要所有这些,所以你的循环应该单独运行这个部分:
foreach ($content['playerstats']['stats'] as $stat) {
$stats[$stat["name"]] = $stat["value"];
}
// Continue as you did...
This will then bring the entire data from your request into the format that you expect, so the second access to $stats
will work properly.
然后,这会将您请求中的所有数据转换为您期望的格式,因此第二次访问$ stats将正常工作。
#2
0
I think your insert statement needs to be inside the foreach, try this.
我认为你的insert语句需要在foreach中,试试这个。
$con = mysqli_connect("mysql6.000webhost.com", "a2689951_steam", "password", "a2689951_playerdata");
$stats = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=(MyKey)&steamid=(PlayerID)");
$json = json_decode($stats, true);
$stats = array();
foreach ($content['playerstats']['stats'] as $stat) {
$stats[$stat["name"]] = $stat["value"];
$total_kills = $stats["total_kills"];
$total_deaths = $stats["total_deaths"];
$insert = "INSERT INTO user (total_kills, total_deaths) VALUES ('$statement', 'siss', $total_kills, $total_deaths)";
if ($con->query($insert) === TRUE) {
$response = array();
json_encode($response);
return json_encode($response["success"]);
} else {
return "Error: <br>" . $con->error;
}
}
#1
0
A couple of things to get into the habit of doing, since you're a beginner:
因为你是一个初学者,有几件事要养成做的习惯:
- Don't use the
mysql
-functions in PHP. Usemysqli
instead (it even supports object-orientet notation!) - 不要在PHP中使用mysql函数。使用mysqli(它甚至支持object-orientet表示法!)
- The way you have your
INSERT
-statement set up makes your vornable to SQL Injection attacks. Use a "prepared statement" instead. - 设置INSERT语句的方式使您可以进行SQL注入攻击。请改用“预备声明”。
- To "return" data from a PHP script (that is, send it directly to the browser), you need to use
echo
instead ofreturn
. - 要从PHP脚本“返回”数据(即将其直接发送到浏览器),您需要使用echo而不是return。
- You have this line in your code:
json_encode($response)
, where$response
is an empty array. This line won't do anything. thejson_encode()
-function returns a String that contains the JSON-representation of whatever you give it. It does not change it! - 你的代码中有这一行:json_encode($ response),其中$ response是一个空数组。这条线不会做任何事情。 json_encode() - 函数返回一个String,其中包含您提供的任何内容的JSON表示。它没有改变它!
- I removed the API-Key from the URL you had in your example, since sharing the key publicly is not a good idea and can result in Valve banning your key (for example if malicious people use it to send requests to the API).
- 我从您示例中的URL中删除了API-Key,因为公开共享密钥并不是一个好主意,并且可能导致Valve禁止您的密钥(例如,如果恶意用户使用它向API发送请求)。
Now for your actual problem:
现在针对您的实际问题:
foreach ($json->playerstats->stats as $stat) {
$stats[$stat["name"]] = $stat["value"];
$total_kills = $stats["total_kills"];
$total_deaths = $stats["total_deaths"];
I think there might be some confusion as to what part of this code is actually repeated by the loop. It's not just the first line, it's the entire body (enclosed by the {}
-brackets).
我认为这个代码的哪一部分实际上被循环重复可能会有些混乱。它不仅仅是第一行,它是整个主体(由{} -brackets包围)。
The problem is that you will only get the first entry from the JSON into the $stats
-array, which would be total_kills
. But you want all of them, so your loop should run over this part alone:
问题是你只能从JSON获得第一个条目到$ stats-array,这将是total_kills。但是你想要所有这些,所以你的循环应该单独运行这个部分:
foreach ($content['playerstats']['stats'] as $stat) {
$stats[$stat["name"]] = $stat["value"];
}
// Continue as you did...
This will then bring the entire data from your request into the format that you expect, so the second access to $stats
will work properly.
然后,这会将您请求中的所有数据转换为您期望的格式,因此第二次访问$ stats将正常工作。
#2
0
I think your insert statement needs to be inside the foreach, try this.
我认为你的insert语句需要在foreach中,试试这个。
$con = mysqli_connect("mysql6.000webhost.com", "a2689951_steam", "password", "a2689951_playerdata");
$stats = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=(MyKey)&steamid=(PlayerID)");
$json = json_decode($stats, true);
$stats = array();
foreach ($content['playerstats']['stats'] as $stat) {
$stats[$stat["name"]] = $stat["value"];
$total_kills = $stats["total_kills"];
$total_deaths = $stats["total_deaths"];
$insert = "INSERT INTO user (total_kills, total_deaths) VALUES ('$statement', 'siss', $total_kills, $total_deaths)";
if ($con->query($insert) === TRUE) {
$response = array();
json_encode($response);
return json_encode($response["success"]);
} else {
return "Error: <br>" . $con->error;
}
}