[英]Query table and then query another table for each result and then … etc
I need to query a database where location = $location
. 我需要查询location = $location
的数据库。 This will return some user_id
s. 这将返回一些user_id
。
I then need to query another database where user_id
= those previous user_id
results. 然后,我需要查询另一个数据库,其中user_id
=先前的user_id
结果。 This will find game_id
s. 这将找到game_id
。
I then need to query another table with those game_id
s to get the name of each game (each game name is assigned with an id.) 然后,我需要使用这些game_id
查询另一个表以获取每个游戏的名称(每个游戏名称都分配了一个ID。)
This what I have: 这是我所拥有的:
if (isset($_POST['location'])) {
$location = $_POST['location'];
$query = mysql_query("SELECT * FROM users WHERE location = '$location'") or die(mysql_error());
echo "<ul>";
while ($row = mysql_fetch_array($query)) {
foreach (explode(", ", $row['user_id']) as $users) {
echo "<li>" . $row['user_name'] . "<ul><li>";
//this where I need I need to query another table where user_Id = $user (each user found in previous query)
//this will find multiple game id's (as $game) , I then need to query another table to find game_id = $game
// output name of game
echo "</li></ul></li>";
}
}
echo "</ul>";
}
Sounds complicated. 听起来很复杂。 Is there a simpler way? 有没有更简单的方法?
Such a query will produce the location, user_id, game_id
table. 这样的查询将产生location, user_id, game_id
表。 Put as much effort in your SQL query so that you will be able to handle it easily by your code: 在您的SQL查询中投入尽可能多的精力,以便您可以通过代码轻松处理它:
$query = mysql_query("SELECT game_name FROM another_table \
JOIN users \
JOIN games \
WHERE users.location = '$location'")
or die(mysql_error());
while ($row = mysql_fetch_array($query)) {
echo $row['game_name']; //Here is the game name
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.