从数据库中 id 与数组匹配的两个表中获取信息


Get info from two tables in database where id match an array?

我有两个表,一个是存储不同价格的价格:

prices
appid      de      us      ru      nl      gb
29382      899     999     1299     899    699
48371     1299    1599     1899    1299    999
58193      699     899      999     899    599 

其他表是存储有关游戏的各种信息的游戏:

games
appid      title      releasedate      controler      language
29382     title 1     1358197200           1             en
48371     title 2     1329858000           0             en
58193     title 3     1201554000           1             en

如您所见,两个表具有相同的appid,我需要将其与json文件中的外部id匹配

最后我有一个 json 文件,如下所示:

{
"response": {
    "games": [
        {
            "appid": 58193,
            "playtime_forever": 0
        },
        {
            "appid": 29382,
            "playtime_forever": 0
        },
        {
            "appid": 48371,
            "playtime_forever": 151
        }
    ]
}
}

现在,如果这只是基于 json 字段从一个表中提取信息,我会知道如何做,但对我来说这似乎有点复杂。我尝试了各种解决方案,但从未得到有效的解决方案。

我现在拥有的最终代码是这样的:

$listusergames = json_decode(file_get_contents('http://path_to_file.json'));
    foreach ($listusergames->response->games as $game) {
        $gameid = $game->appid;
        $querygamename = mysqli_query($conn, "SELECT * FROM games WHERE appid='$gameid'");
        $rowgame = mysqli_fetch_array($querygamename);
        if($rowgame[0] > 1 ) {
        echo 'Your game: ' . $rowgame['title'] . ' is worth $' . $rowgame['us'] . ' and game id is - '. $gameid . '<br/>';
        }
    }

在此代码中,$rowgame['us'] 现在无效,因为我不知道如何根据 json 字段从两个表中提取数据。

在我现在拥有的解决方案中,每个appid都是从json文件中提取的,然后我根据json文件中的appid值从游戏表中选择了所有值,并使用if($rowgame[0]> 1(,因为其他wite它将回显数据库中不存在的json文件中的每个appid的空行。

但是我不知道如何选择两个表并从一个表中显示游戏标题,从另一个表中显示不同的价格,并将它们与json文件中的appid匹配,如果数据库中不存在,请跳过该appid。

编辑:另一个快速的问题,由于mysql_query在foreach循环中,这不会消耗太多资源,因为查询将针对每个$game执行,所以如果我有500个游戏,我将为每个循环执行500个查询,并使我的网站非常慢,或者我错了?

更新了查询以联接两个表:

SELECT g.*, p.* FROM games g 
LEFT JOIN prices p on (g.appid = p.appid) 
WHERE g.appid='$gameid'

传统上,您可以将 JSON 结果中的数组implode()到一个字符串中,并使用 WHERE/IN SQL 语句。但是,由于您正在查看对象的 appid 参数,而不是一个简单的数组,因此如果不付出一些额外的努力,就无法implode()

您可以foreach() JSON 结果来构建查询,并只执行一个 MySQL 语句,这将节省一些开销。

完整更新的代码:

// Grab JSON list
$listusergames = json_decode(file_get_contents('http://path_to_file.json'));
// Setup new array
$games = array();
// Add member to array for each game's appid
// Resulting array looks like ( [0]=>'58193', [1]=>'29382', [2]=>'48371 )
foreach ($listusergames->response->games as $game) {
    $games[] = $game->appid;
}
// Implode games array into a comma-separated string like: "58193,29382,48371"
$games_list = implode( ',' , $games );
// Updated SQL statement using WHERE/IN list
$querygamename = mysqli_query($conn, "SELECT g.*, p.* FROM games g 
LEFT JOIN prices p on (g.appid = p.appid) 
WHERE g.appid IN ($games_list) ");
// Loop through result set
while ( $rowgame = mysqli_fetch_array($querygamename) ){
    if($rowgame[0] > 1 ) {
        echo 'Your game: ' . $rowgame['title'] . ' is worth $' . $rowgame['us'] . ' and game id is - '. $gameid . '<br/>';
    }
}