简体   繁体   English

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

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

I have a two tables and one is prices which stores different prices: 我有两个表,一个是存储不同价格的价格:

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 

And other table is games which stores various info about games: 其他表格是游戏,其中存储有关游戏的各种信息:

games

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

As you can see both tables have same appid and i need to match that with outer id's from json file 如您所见,两个表具有相同的appid,我需要将其与json文件中的外部id匹配

And at the end i have a json file which looks like this: 最后,我有一个看起来像这样的json文件:

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

}
}

Now if this was just pull an info from one table based on json field i would know how to do it but for me this seems a bit complicated. 现在,如果这只是从一个基于json字段的表中提取信息,我就会知道该怎么做,但对我来说,这似乎有点复杂。 I tried various solutions but never got an working one. 我尝试了各种解决方案,但从未找到可行的解决方案。

The final code i have now is this: 我现在拥有的最终代码是:

$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/>';
        }
    }

In this code $rowgame['us'] is invalid right now because i don't know how to pull data from both tables based on json field. 在这段代码中,$ rowgame ['us']现在无效,因为我不知道如何基于json字段从两个表中提取数据。

In the solution that i have right now, each appid is pulled from json file and then i selected all values from games table based on appid values from json file, and used if($rowgame[0] > 1 ) because otherwite it will echo empty line for each appid from json file that doesn't exist in database. 在我现在拥有的解决方案中,每个appid都从json文件中提取,然后我根据json文件中的appid值从游戏表中选择了所有值,并使用了if($ rowgame [0]> 1),因为否则它将回显数据库中不存在的json文件中每个appid的空行。

But i don't know how to select both tables and display game title from one table and different prices from other table and match them with appid from json file and if it doesn't exist in database skip that appid. 但是我不知道如何选择两个表并从一个表中显示游戏标题,以及从另一个表中显示不同的价格,并将它们与json文件中的appid匹配,如果数据库中不存在appid,则跳过该appid。

Edit: One more quick question, since mysql_query is in foreach loop wouldn't that consume too much resources since query will be executed for each $game so if i have 500 games i will have 500 queries for every loop, and making my site very slow, or im wrong ? 编辑:一个更快的问题,因为mysql_query在foreach循环中不会消耗太多资源,因为将对每个$ game执行查询,所以如果我有500个游戏,则每个循环将有500个查询,这使我的网站非常有用慢,还是我错了?

Updated query to JOIN both tables: 更新查询以联接两个表:

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

Traditionally you could implode() an array from your JSON results into one string, and use WHERE/IN SQL statements. 传统上,您可以将JSON结果中的数组implode()成一个字符串,然后使用WHERE / IN SQL语句。 But, because you are looking at the object's appid parameter, and not a simple array, you cannot implode() without a bit of extra effort. 但是,由于您正在查看对象的appid参数,而不是简单的数组,因此,如果不付出额外的努力,就无法实现implode()

You could foreach() through the JSON results to build your query, and execute just one MySQL statement which will save some overhead. 您可以通过JSON结果foreach()来构建查询,并仅执行一条MySQL语句即可节省一些开销。

Full updated code: 完整的更新代码:

// 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/>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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