无法在mysql中查询两个表

时间:2013-06-13 17:42:40

标签: php mysql

如何显示所有教程_post以及发布帖子的用户的用户名...我想看看sql语句的外观如何

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情:

$postID = 1;//Post id we want to get from the database
$getPost = $connection->query("SELECT * FROM posts WHERE post_id='$postID'");//Get the post by the id
$post = $getPost->fetch_assoc();//Fetch the result to an array
$getUser = $connection->query("SELECT username FROM users WHERE user_id=".$post['id']);//Get the username by using the id we got from the $post['user_id']
$user = $getUser->fetch_assoc();//Fetch the second result to an array

//Print everything
echo "Title:".$post['title']."<br/>";
echo "Username:".$user['username']."<br/>";
echo "Body:".$post['body'];

我无法真正看到表格的名称,所以我猜对了。