我能以某种方式仅使用1个sql查询吗?
showthread.php
// Get Topic subject etc
$threadID = isset($_GET['threadID']) ? intval($_GET['threadID']) : 0;
$result = mysql_query("SELECT * FROM topics WHERE id = $threadID");
// Fetch rows
$row = mysql_fetch_assoc($result);
$subject = htmlspecialchars($row['subject']);
echo '<h2>'.$subject.'</h2>';
// Get posts that belong to this topic!
$posts = mysql_query("SELECT * FROM posts INNER JOIN users ON users.id = posts.user_id WHERE posts.topic_id = $threadID");
// posts.....
while ($post = mysql_fetch_assoc($posts)) {
echo '<br>'.$post['message'].'';
}
答案 0 :(得分:2)
根据我的理解,你应该能够从这样的东西中获得所需的所有信息吗? (加上或减去任何缺失的列)
SELECT topics.subject, posts.message
FROM posts
INNER JOIN users ON users.id = posts.user_id
INNER JOIN topics ON topics.id = posts.topic_id
WHERE posts.topic_id = $threadID
答案 1 :(得分:1)
使用mysqli
使用real_escape_string
使用Prepared语句或PDO