[英]Query table and then query another table for each result and then … etc
我需要查詢location = $location
的數據庫。 這將返回一些user_id
。
然后,我需要查詢另一個數據庫,其中user_id
=先前的user_id
結果。 這將找到game_id
。
然后,我需要使用這些game_id
查詢另一個表以獲取每個游戲的名稱(每個游戲名稱都分配了一個ID。)
這是我所擁有的:
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>";
}
聽起來很復雜。 有沒有更簡單的方法?
這樣的查詢將產生location, user_id, game_id
表。 在您的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.